100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
cos 1512 exam pack $2.98   Add to cart

Exam (elaborations)

cos 1512 exam pack

 14 views  1 purchase
  • Course
  • Institution

cos 1512 exam packcos 1512 exam packcos 1512 exam pack

Preview 4 out of 55  pages

  • September 20, 2021
  • 55
  • 2021/2022
  • Exam (elaborations)
  • Questions & answers
avatar-seller
JUNE 2017 P2

1 C STRINGS
Error Correction
If(str!=”STOP” If(strcmp(str,”STOP”);
Str = “STOP” Strcpy(str,”STOP”);



2 RECURSION
Void CountDown(int num)
{
If (num = 0)
Cout << 0 << endl;
Else
{
Cout << num << endl;
CountDown(num-1);
}
}


3 POINTERS

3.1 The problem with this this code fragment is that the code deletes the pointer.
Thereafter, the programmer is assigning the value to a pointer that doesn’t point to
anything. The results are disastrous.

3.2 Correct the Code

3.2.1 5 : p1 = &x; 6 : p2 = &y;
3.2.2 7: cout << *p1 << “ “ << *p2;
3.2.3 9: 30

3.3 typedef int* myPtr;
myPtr = a;
int size’;
a = new int[size];


4 DEFINE PRODUCT CLASS

Product.h

#ifndef PRODUCT_H
#define PRODUCT_H

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>


Page 1 of 6

,JUNE 2017 P2

using namespace std;


class Product
{
public:
Product();
~Product();
long getNumber();
void reset(long rID, double rPrice, long rNum);
void increaseRetailPrice(float incPercent);
friend bool operator ==(const Product &p1, const Product &p2);
friend Product operator -- (Product &P);
friend istream& operator >> (istream& ins, Product &P);
friend ostream& operator << (ostream& outs, const Product &P);


protected:

private:
long id;
double price;
long number;
};

#endif // PRODUCT_H

Product.CPP
#include "Product.h"
#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

Product::Product()
{
id = 0;
price = 0.0;
number = 0;

}

Product::~Product()
{
//dtor
}

long Product::getNumber()

Page 2 of 6

,JUNE 2017 P2
{
return number;
}

void Product::reset(long rID, double rPrice, long rNum)
{
id = rID;
price = rPrice;
number = rNum;
}

void Product::increaseRetailPrice(float incPercent)
{
price = price + (price * incPercent);

}

bool operator ==(const Product &p1, const Product &p2)
{
if ((p1.id == p2.id) && (p1.price == p2.price) && (p1.number == p2.number))
return true;
else
return false;
}



Product operator-- (Product &P)
{

--P.number;

return P;


}

istream& operator >> (istream& ins, Product &P)
{
ins >> P.id >> P.price >> P.number;
//ins >> P.price;
//ins >> P.number;
return ins;
}

ostream& operator << (ostream& outs, const Product &P)
{
outs << P.id << endl;
outs << P.price << endl;
outs << P.number << endl;
return outs;

Page 3 of 6

, JUNE 2017 P2
}

Main()

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "Product.h" //1


using namespace std;

int main()
{
ifstream input;//2
input.open("Products.txt");//3
if (input.fail())
{
cout << "File failed to open " ;
exit(1);
}
Product P1, P2;
long ID, Nr;
float Price;
cout <<"Enter product ID: ";
cin >> ID;
cout <<"Enter wholsesale Product price: ";
cin >> Price;
cout << "Enter number in stock of product" << "ID: ";
cin >> Nr;
P1.reset(ID,Price,Nr);//4
while(!input.eof()) // 5
{
input >> P2 ;//6
cout << P2.number; //6
}
if (P1 == P2) // 7
{
P2.increaseRetailPrice(0.05); //8
--P2; //9
}

cout << P1 << endl;
cout << P2 << endl;

input.close();

return 0;
}



Page 4 of 6

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller JABULANIKHOLWA. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $2.98. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

75323 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$2.98  1x  sold
  • (0)
  Add to cart