Write your question here.
Test Monday, trying to do this example and I can't finish it here's the Example Exercise #1:
Declare a structure called Student which contains:
- two integer variables (fields), called numRes and numNonRes
Their values will represent the number of resident and non-resident students, respectively
- a no-argument Student constructor which initializes the two integer data fields to zero
- a member function, called displayResCount
This function displays the number of resident and non-resident students based on the values of numRes and numNonRes (See sample run below)
Also declare (prototype) and define (implement):
- two stand alone functions called inputArray and countValues
Descriptions are given below
The main function:
Declares
(1) a character pointer called resArray. This will be a character array ( NOT be a c-string).
(2) an integer variable called size which will represent the number of elements in resArray
(3) an instance of the Student structure called stu
Dynamically creates the character array resArray (which will NOT be a c-string) of size number of elements
Calls
- inputArray to assign to each element of resArray the character inputted by the user.
You may assume the user enters either 'r' for resident or 'n' for non-resident. No input validation. See sample run below.
- countValues to assign values to the menber variables numRes and numNonRes of stu based on the values in resArray.
For example, in the sample run below, there are 3 r's and 2 n's in resArray, so in stu, numRes would be 3, and numNoRes would be 2.
- displayResCount (member function of stu) to display the number of resident and nonresident students. See sample run.
The displayed numbers are based on the values of numRes and numNonRes in stu.
//Here is the skeletal code to start you out
//Declare structure Student (as described above)
//prototype function inputArray, with proper parameters
//prototype function countValues, with proper parameters
int main()
{
char* resArray;
int size;
Student stu;
cout << "Enter the number of input values: ";
cin >> size;
//dynamically create resArray with size elements
//call function inputArray
// call function countValues
// call member function displayResCount as a member of stu
return 0;
}
I don't understand why? and how to use the "count values " function. Here's what I have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int intnumRes, numNonRes;
Student()
{
intnumRes = 0;
numNonRes = 0;
}
Student displayResCount()
{
cout << "Number of Res: " << intnumRes << endl;
cout << "Number of noneRes: " << numNonRes << endl;
}
};
//prototype function inputArray, with proper parameters
void inputArray(char[],int);
//prototype function countValues, with proper parameters
int Countvalues(int , int);
int main()
{
char* resArray;
int size;
Student *stu;
cout << "Enter the number of input values: ";
cin >> size;
//dynamically create resArray with size elements
resArray = new char[size];
//call function inputArray
inputArray(resArray,size);
// call function countValues
// call member function displayResCount as a member of stu
for (int x = 0;x < size;x++)
{
stu[x].intnumRes;
stu[x].numNonRes;
}
return 0;
}
void inputArray(char[],int num)
{
int y = 0, J = 0;
char choice;
Student totRes;
Student NoneRes;
for (int X = 0;X < num;X++)
{
cout << "Please enter R for resident or N for noneresident: ";
cin >> choice;
if (choice == 'R' || choice == 'r')
{
totRes.intnumRes;
y++;
}
else if (choice == 'N' || choice == 'n')
{
NoneRes.numNonRes;
J++;
}
}
cout << y << endl; \\ Ran a simple cout statement to show how many times the user was pressing "Choice"
cout << J << endl;
}
int Countvalues(int numRes, int nonRes)
{
Student total1;
Student Total2;
int Total;
\\ don't understand how and why we use this function
total1.intnumRes = numRes;
Total2.numNonRes = nonRes;
Total = numRes;
return Total;
}
Put the code you need help with here.
|