Functional Decomposition: Math tutor Project

Hi guys this is my first C++ project, and it deals with functional decomposition. I am having an insanely difficult time trying to figure out how to solve this problem which is why I am here asking for help. I have posted what I have below but it is not much :(

Aissignment:http://srjcstaff.santarosa.edu/~dharden/22/proj1.html
Project 1: Math Tutor

Project 1.1

Participate in the lesson 7 discussion. This could involve asking a question, answering another student's question, giving an example of something that you struggled with and then overcame (or didn't!), giving an example of something you found particularly cool, or any other constructive way you can think of to participate.
Project 1.2

Do Dale chapter 7 EPE 1 - 14. Write the answers neatly or type them up, and staple them to the back of this assignment. (Online students: type them up and paste them into your e-mail message.)

Project 1.3

This program will ask the user to answer a series of arithmetic problems and report on how the user performs. You will write this program in the following phases. Make sure that each phase works correctly and is stylistically perfect before progressing to the following phase.

Note that the purpose of this assignment is to give you lots of practice with parameter passing without making you write a huge program. For this reason I have provided a structure diagram at the end of this document. Make sure that you adhere to the structure diagram carefully.

Turn in only your final product.

Phase I: Your main function for phase I must look exactly like this:




int main()
{
srand(time(0));
doOneSet();
}


You must write the function doOneSet which will, for now, write out 5 addition problems. All of the numbers in your programs should be between 0 and 100. Here is the sample output for this phase:




45 + 21 =
0 + 100 =
54 + 23 =
4 + 19 =
18 + 92 =


The numbers that are produced for these problems must be generated randomly by the computer. The numbers in the sample output are given only as an example. We will discuss in the lessons how to generate random numbers.

Phase II: Change your doOneSet function so that instead of just writing out the problems it also allows the user to enter an answer, and then tells the user whether the answer was correct or not. Do not change your main function. Here is the sample output for this phase (user input is indicated here by using bold font. It won't be bold in your own output):




45 + 21 = 66
correct
0 + 100 = 100
correct
54 + 23 = 67
incorrect
4 + 19 = 13
incorrect
18 + 92 = 100
correct



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
//********************************
// This program will ask the user to answer a series of
//arithmetic questions and will report on how the user performs.

#include <iostream>
#include <ctime> // Will be used by the srand function for time
#include <cstdlib> // will allow for random numbers to be generated.

using namespace std;
void getProbsPerSet(int& numProbs);
// The following functions will have inputs
void doOneSet( char problemType,int numProbs,int& correctCount);
void getMaxNum(int &maxNum); // Will need inputs
void printHeader(char problemType);
void doOneProblem(char problemType, int& MaxNum, int & correctCount);
void generateOperands(int& firstNum, int& secondNum, int maxNum);
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);
void checkAnswer (int correctAnsw,int answer, int&correctCount);
void printReport(int probsPerSet, int set1Correct, int set2Correct, int set3Correct);

int main()
{
    srand(time(0));

    doOneSet();
}
 void doOneSet(char problemType, int& maxNum, int& correctCount)
 // Pre: problemType, maxNum and correctCount are assigned.
 // Post: will output a set of problems.

 {
     // Local Variable declaration
     int& maxNum;
     //function will call
     printHeader(problemType);
     getMaxNum(maxNum);

     // Loop to output a set of 5 random problems
     int firstNum, secondNum, sum

     for (int i = 0; i < 5; ++i) // restrict to display 5 problems ONLY!
     {
        int firstNum = rand() % (maxNum + 1);
        secondNum = rand() % ( maxNum + 1);
        
        sum= firstNu + secondNum;
        std::cout << firstNum<< " + " << secondNum << " = ";
        std:: cin >> correctCount
        
     }  
 }


 
Last edited on
references within program, shout if anything's unclear:
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
#include <iostream>
#include <random>
#include <vector>
#include <chrono>//time
#include <utility>//std::move(), std::make_pair
#include <iomanip>//std::width(), std::left;

void doOneSet();

int main()
{
   //srand(time(0));
   //rand() not the best C++ has to offer http://stackoverflow.com/questions/26440252/is-rand-really-that-bad
   doOneSet();
}
void doOneSet()
{
    std::default_random_engine numGenerator(static_cast<unsigned int>(time(NULL)));
   //http://www.cplusplus.com/reference/random/default_random_engine/

    std::uniform_int_distribution<int> range(0, 100);
    //http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

    std::vector<std::pair<int,int>> randomSums{};

    for (int i = 0; i < 5; ++i)
    {
        auto tempPair = std::make_pair(range(numGenerator),range(numGenerator));
        //http://en.cppreference.com/w/cpp/utility/pair/make_pair
        randomSums.push_back(std::move(tempPair));
    }
    for (const auto& elem : randomSums)
    {
        std::cout.width(2); std::cout << std::left << elem.first << " + ";
        std::cout.width(2);std::cout << std::left << elem.second << " = ??\n";
        std::cout << "Enter your guess \n";
        int guess{};
        std::cin >> guess;
        std::cout << ( (guess == elem.first + elem.second) ? "Correct! \n" : "Incorrect! \n" );
    }
}

btw, this is incorrect:
18 + 92 = 100
correct
Last edited on
Hi,

The declaration:

void generateOperands(int& firstNum, int& secondNum, int maxNum);

The definition:

1
2
void generateOperands (int& firstNum,int& secondNum,int& maxNum)
{
So wrote this I am getting errors on lines 58,59 61,74,
I have been working on this for quite some time so my mind is somewhat clogged and I might be missing my errors.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//********************************
// This program will ask the user to answer a series of
//arithmetic questions and will report on how the user performs.

#include <iostream>
#include <ctime> // Will be used by the srand function for time
#include <cstdlib> // will allow for random numbers to be generated.

using namespace std;
void getProbsPerSet(int& numProbs);
// The following functions will have inputs
void doOneSet( char problemType,int numProbs,int& correctCount);
void getMaxNum(int &maxNum); // Will need inputs
void printHeader(char problemType);
void doOneProblem(char problemType, int& MaxNum, int & correctCount);
void generateOperands(int& firstNum, int& secondNum, int maxNum);
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);
void checkAnswer (int correctAnsw,int answer, int&correctCount);
void printReport(int probsPerSet, int set1Correct, int set2Correct, int set3Correct);

int main()
{

    int probsPerSet;
    int set1Correct,set2Correct,set3Correct;
    srand(time(0)); // will initialize the random number generator
    getProbsPerSet(probsPerSet);
    doOneSet('+',probsPerSet,set1Correct);
    doOneSet('-',probsPerSet,set2Correct);
    doOneSet('*',probsPerSet,set3Correct);
 return 0;
}


void getProbPerSet(int& numProbs)
{
    do
    {
        cout<< " Enter the number of problems per set you would like (between 3 and 10):";
        cin >> numProbs;
    }
    while (numProbs< 3 || numProbs > 10); // Restrict the user to no less than 3 sets but no more than 10
}




void doOneSet( char problemType,int numProbs,int& correctCount);

    // declare variables
    int maxNum;
    // function will call:
     printHeader(char problemType);
     getMaxNum(maxNum);

     for(int = 1; i <= numProbs; i++)
     {
         doOneProblem(sign, maxNum ,correctCount);


     }






void printHeader(char problemType);
{
  switch(problemType)
    {
        case '+' : cout << "\nSet Number One"<< endl
                  << "----------" << endl;
                  break;
        case '*' : cout << "\n Set Number Two"<< endl
                  << "----------" << endl;
                  break;

        case '-' : cout << "\n Set Number Two"<< endl
                  << "----------" << endl;
                  break;
        default : cout << "incorrect";

    }
}



void getMaxNum(int &maxNum);
{
    cout << " What is the maximum number for this set?:";
    cin >> maxNum
}



void doOneProblem(char problemType, int& MaxNum, int & correctCount);
{
    // assign variables
    int firstNum, secondNum; // random numbers the program will be using
    int answer;
    int correctAnswer = 0;

    getNumbers(firstNum, secondNum, maxNum);

    cout << firstNum <<" " << problemType << " " << secondNum << " = ";
    cin >> userAnswer;

    void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);

    void checkAnswer (int correctAnsw,int answer, int&correctCount);

}
void getMaxNum(int &maxNum);
{
    value1 = rand() % (max_range +1);
    value2 = rand() % (max_range +1);

}

void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);

 switch(arithmProblemType)
    {
        case '+' : value3 = value1 + value2;
        break;
        case '-' : value3 = value1 - value2;
        break;
        case '*' : value3 = value1 * value2;
        break;
        default : cout << "incorrect";
    }
}

void checkAnswer (int correctAnsw,int answer, int&correctCount);

{
    if(userValue == correctValue)
    {
        cout << "correct" << endl;
        countCorrect++;
    }
    else
        cout << "incorrect" << endl;
}

void printReport(int probsPerSet, int set1Correct, int set2Correct, int set3Correct);
{
    // explicit type casting is performed for the percentage indicator.
    cout << "\nSet #1: You got " << set1Correct << " correct out of "
        << probsPerSet << " for " << int(set1Correct * 100.0 / probsPerSet + 0.5)
        << "%." << endl;

    cout << "Set #2: You got " << set2Correct << " correct out of "
        << probsPerSet << " for " << int(set2Correct * 100.0 / probsPerSet + 0.5)
        << "%." << endl;

    cout << "Set #3: You got " << set3Correct << " correct out of "
        << probsPerSet << " for " << int(set3Correct * 100.0 / probsPerSet + 0.5)
        << "%." << endl;

    cout << "Overall you got " << set1Correct + set2Correct + set3Correct << " out of "
        << probsPerSet * 3 << " for "
        << int(((set1Correct + set2Correct + set3Correct) * 100.0 ) / (probsPerSet * 3) + 0.5)
        << "%." << endl;
}



















The fundamental problem here is that you've written too much code without getting it to compile. Until you become fluent in the syntax, you should compile your code frequently and fix the compilation errors.

Functions can be declared, they can be called, and they can be defined. The syntax for each use is slightly different.

At line 10-19 you correctly declare the functions. In a declaration you give the function return type, the name, the parameters and their types, and you end it with a semicolon.

At line 109 and 111, you are declaring some functions again. Do you mean to call them?

To define a function, you give the return type, parameters, and the parameter types, but instead of ending it with a semicolon, you follow it with the the code, enclosed in {}. Lines 48, 68, 89, etc all have a semicolon at the end of the line. Remove it.

You have 2 definitions of getMaxNum()

There are probably more.
Some other errors:
Line 35: get ProbPerSet or getProbsPerSet?
Line 49: add {
Topic archived. No new replies allowed.