Simple 'class' related homework problems

S

shumakergreg

Hey I'm trying to figure out how to solve these three problems... I
wrote solutions but they all have bugs I can figure out. The basic
idea is that you should only need to write the functions to get the
programs to work, you shouldn't need to modify the main program

3. Write the implementation functions for a class to hold and display
strings. The constructor for each string includes the number of
characters the string is to contain and a specification of either a
single character to be repeated in the string or two characters where
the characters of the string are repeated sequences of all the
consecutive ASCII characters between the first and second character.
See the example output for details. The main program following the
class definition produces the output shown when used with the class.

class Sequence
{
public:
Sequence(int n, char c);
Sequence(int n, char c1, char c2);
void displaySequence();
private:
string s;
};

int main()
{
Sequence *seq;

string s;
int lgt;

// Do
do
{
cout << "Length:";
cin >> lgt;
if (lgt > 0)
{
cin.ignore(10000, '\n');
cout << "String:";
getline(cin, s);
if (s.length() > 0)
{
if (s.length() < 2)
{
seq = new Sequence(lgt, s[0]);
}
else
{
seq = new Sequence(lgt, s[0], s[1]);
}
seq->displaySequence();
delete seq;
}
}
cout << endl;
}
while (lgt > 0);
return 0;
}

Output:

Length:5
String:c
ccccc

Length:8
String:x
xxxxxxxx

Length:10
String:ac
abcabcabca

Length:11
String:df
defdefdefde

Length:12
String:km
klmklmklmklm

Length:16
String:ad
abcdabcdabcdabcd

Length:16
String:ae
abcdeabcdeabcdea

Length:16
String:af
abcdefabcdefabcd

Length:16
String:ag
abcdefgabcdefgab

Length:16
String:zz
zzzzzzzzzzzzzzzz

Length:0

4. Write the implementation functions for a class to hold and display
gas mileage data. Use the class definition provided below. Use the
GasData struct given to hold the data for each gas stop. The miles
member of the structure holds the odometer reading at the time gas is
purchased. The initial mileage is a parameter of the constructor. An
optional second constructor parameter can be used to specify an
initial size for the dynamic array that holds the data. The main
program following the class definition produces the output shown when
used with the class.

struct GasData
{
float miles;
float gallons;
};


class Mileage
{
public:
Mileage(float initialMileage, int initialSize=2);
void addData(float mil, float gals);
void displayAllData();
void displayMileage();
private:
int size;
int count;
GasData *data;
float initialM;
void doubleArray();
};

int main()
{
Mileage md(1000);


md.addData(1200, 10);
md.addData(1500, 10);
md.addData(1750, 12.5);
md.addData(1900, 5);
md.addData(2000, 12.5);

md.displayAllData();

md.displayMileage();

return 0;
}


Output:

Mileage: 1200.0 Miles:200.0 Gallons:10.0 MilesPerGallon:20.0
Mileage: 1500.0 Miles:300.0 Gallons:10.0 MilesPerGallon:30.0
Mileage: 1750.0 Miles:250.0 Gallons:12.5 MilesPerGallon:20.0
Mileage: 1900.0 Miles:150.0 Gallons: 5.0 MilesPerGallon:30.0
Mileage: 2000.0 Miles:100.0 Gallons:12.5 MilesPerGallon: 8.0

Miles Per Gallon = 20.0


5. Write the implementation functions for a class to hold and display
objects that hold a single integer value. The integer is generated
from two static variables. The first static variable last is
initialized to zero and holds the last number generated. The second
static variable inc is initialized to one and holds the increment for
the next number to be generated. After each number is generated, the
variable inc is incremented by one. Include code to initialize the
static variables in your implementations. The main program following
the class definition produces the output shown when used with the
class.

class Number
{
public:
Number();
void displayNumber();
private:
static int last;
static int inc;
int num;
};

int main()
{
Number *pn;

int i;
int limit = 10;

for (i =0; i < limit; i++)
{
pn = new Number;
pn->displayNumber();
delete pn;
}

cout << endl;

return 0;
}

Output:

Number:1
Number:3
Number:6
Number:10
Number:15
Number:21
Number:28
Number:36
Number:45
Number:55
 
C

Christopher

Hey I'm trying to figure out how to solve these three problems... I
wrote solutions but they all have bugs I can figure out. The basic
idea is that you should only need to write the functions to get the
programs to work, you shouldn't need to modify the main program

3. Write the implementation functions for a class to hold and display
strings. The constructor for each string includes the number of
characters the string is to contain and a specification of either a
single character to be repeated in the string or two characters where
the characters of the string are repeated sequences of all the
consecutive ASCII characters between the first and second character.
See the example output for details. The main program following the
class definition produces the output shown when used with the class.

class Sequence
{
public:
Sequence(int n, char c);
Sequence(int n, char c1, char c2);
void displaySequence();
private:
string s;

};

int main()
{
Sequence *seq;

string s;
int lgt;

// Do
do
{
cout << "Length:";
cin >> lgt;
if (lgt > 0)
{
cin.ignore(10000, '\n');
cout << "String:";
getline(cin, s);
if (s.length() > 0)
{
if (s.length() < 2)
{
seq = new Sequence(lgt, s[0]);
}
else
{
seq = new Sequence(lgt, s[0], s[1]);
}
seq->displaySequence();
delete seq;
}
}
cout << endl;
}
while (lgt > 0);
return 0;

}

Output:

Length:5
String:c
ccccc

Length:8
String:x
xxxxxxxx

Length:10
String:ac
abcabcabca

Length:11
String:df
defdefdefde

Length:12
String:km
klmklmklmklm

Length:16
String:ad
abcdabcdabcdabcd

Length:16
String:ae
abcdeabcdeabcdea

Length:16
String:af
abcdefabcdefabcd

Length:16
String:ag
abcdefgabcdefgab

Length:16
String:zz
zzzzzzzzzzzzzzzz

Length:0

4. Write the implementation functions for a class to hold and display
gas mileage data. Use the class definition provided below. Use the
GasData struct given to hold the data for each gas stop. The miles
member of the structure holds the odometer reading at the time gas is
purchased. The initial mileage is a parameter of the constructor. An
optional second constructor parameter can be used to specify an
initial size for the dynamic array that holds the data. The main
program following the class definition produces the output shown when
used with the class.

struct GasData
{
float miles;
float gallons;

};

class Mileage
{
public:
Mileage(float initialMileage, int initialSize=2);
void addData(float mil, float gals);
void displayAllData();
void displayMileage();
private:
int size;
int count;
GasData *data;
float initialM;
void doubleArray();

};

int main()
{
Mileage md(1000);

md.addData(1200, 10);
md.addData(1500, 10);
md.addData(1750, 12.5);
md.addData(1900, 5);
md.addData(2000, 12.5);

md.displayAllData();

md.displayMileage();

return 0;

}

Output:

Mileage: 1200.0 Miles:200.0 Gallons:10.0 MilesPerGallon:20.0
Mileage: 1500.0 Miles:300.0 Gallons:10.0 MilesPerGallon:30.0
Mileage: 1750.0 Miles:250.0 Gallons:12.5 MilesPerGallon:20.0
Mileage: 1900.0 Miles:150.0 Gallons: 5.0 MilesPerGallon:30.0
Mileage: 2000.0 Miles:100.0 Gallons:12.5 MilesPerGallon: 8.0

Miles Per Gallon = 20.0

5. Write the implementation functions for a class to hold and display
objects that hold a single integer value. The integer is generated
from two static variables. The first static variable last is
initialized to zero and holds the last number generated. The second
static variable inc is initialized to one and holds the increment for
the next number to be generated. After each number is generated, the
variable inc is incremented by one. Include code to initialize the
static variables in your implementations. The main program following
the class definition produces the output shown when used with the
class.

class Number
{
public:
Number();
void displayNumber();
private:
static int last;
static int inc;
int num;

};

int main()
{
Number *pn;

int i;
int limit = 10;

for (i =0; i < limit; i++)
{
pn = new Number;
pn->displayNumber();
delete pn;
}

cout << endl;

return 0;

}

Output:

Number:1
Number:3
Number:6
Number:10
Number:15
Number:21
Number:28
Number:36
Number:45
Number:55

What's the question?
What's the expected behavior?
Have you stepped through your program in a debugger?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top