Help with functional decomposition

Pages: 12
How do I finish this code? I've done the beginning but the rest, I'm clueless as I had had tutoring for the first part.

This is the code so far:

#include <iostream>
using namespace std;

void printInfo();
int getScore ();
double calcAverage (int score1,int score2, int score3);
int main() {
int score1 = 0, score2 = 0, score3 = 0;
double average;

printInfo ();
cout << endl << score1;
cout << score2;
cout << score3 << endl;
cout << endl << "Score 1 : ";
score1 = getScore();
cout << score1 <<endl;
cout << endl << "Score 2 : ";
score2 = getScore();
cout << score2 <<endl;
cout << endl << "Score 3 : ";
score3 = getScore();
cout << score3 <<endl;

}
/*
void printInfo ()
post: Information is posted on the Screen.
*/
void printInfo()
{
cout << "Enter three scores for three different exams.";
cout << "The program will calculate ";
cout << "\nthe average and then display the average.";
}
// Askes user to enter a value and gets score
int getScore ()
{
int score;
cout << "Enter a value: ";
cin >> score;
return score;
}
________________________________________________________________________
These are the directions for what I have left to do:

3) Add statement in main(), after the work done in part 2 above, to call the
calcAverage() passing the three scores and then return the average. It will
be something like this:
average = calcAverage(score1,score2,score3);3
4) Add definition for your calcAverage() function by taking in the three values
passed and then calculate and return the value for average.
5) After the statement done in step 3 of this task (above) add a statement to
print the value for average returned from the calcAverage() function.
6) Run your program and enter three different scores and check that the average
is calculated (and displayed) correctly.
Do continue if this process is not working correctly.
7) Add documentation for your function; what it does, preconditions and
postconditions.
Part 4.
1) Then add a prototype (before the main function) for a function called
printOutput() with the following prototype.
void printOutput(int score1,int score2,int score3,double average);
Note that prototype could also be like this
void printOutput(int ,int ,int ,double);
That is, only the data type is mandatory.
2) In your main function, add a statement (after work done for part 3 above)
that will call the printOutput() function. It will be like this.
printOutput(score1,score2,score3,average);
3) Add definition for your printOutput() function by printing the three scores
and also their averages.
4) Run your program and test that the average is displayed correctly. You still
should have the statement in main() that print the average, and this value
and the value displayed in the printOutput() function should agree.
Do continue if this process is not working correctly.
5) Add documentation for your function; what it does, preconditions and
postconditions.
Lastly, remove all the cout statements in the main (and may be in your functions)
that was put in for debugging purposes.
Last edited on
I can help to you to do the whole exercise, but what -code- do you have now?
You are right, it does lead you through a step at a time. Let's look at the first part:
1) First implement a prototype (before the main function) for a function called
printInfo() with the following prototype.
void printInfo();
2) Write your main() function and add a statement to call the printInfo()
function. That should be the only statement in main()

It actually supplies the code, verbatim for the first part, so that's done for you:
void printInfo();

Then for the next part is says to write function main() and says there is to be only one statement within main().
So the complete program so far looks like this:
1
2
3
4
5
6
void printInfo();

int main()
{
    printInfo();
}


That's steps 1 and 2 completed. Some of the later steps may be a bit harder, but I think you should consider the instructions as helping you complete the project, guiding you through it. Hope this helps.

@eyenrique

I only have steps 1 and 2 finished.
If you are not able to do this assignment, you don't will to pass your course!
study carefully this code! and first chapters of C++ programming!
Best regards!


WRITE WHAT YOUR PROGRAM DOES HERE!

Enter score: 4
Enter score: 19
Enter score: 32

Scores:

Score 1: 4
Score 2: 19
Score 3: 32
------------
Average: 18.3333


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
//Assignment.cpp
//Add description; 

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void printInfo(); //function prototype
int setScore(); 
double getAverage(int score1,int score2,int score3);
void printOutput(int,int,int,double); //parameter names are not mandatory 
//to the function prototype, but please is meaningful to the programmer. 

int main(){

int score1=0;
int score2=0;
int score3=0;

double average;

printInfo(); //function call to printInfo

score1=setScore(); //assign -set- new values 
score2=setScore();
score3=setScore();

average=getAverage(score1,score2,score3); //calculate score average

printOutput(score1,score2,score3,average); //function call to printOutput




return 0; //indicates success
}//end main

//function to print what the program does
void printInfo(){
	cout<<"\nWRITE WHAT YOUR PROGRAM DOES HERE!\n"<<endl;
}//end function printInfo

//function to initialize scores
int setScore(){

int score;
cout<<"Enter score: ";
cin>>score;

return score;	
}//end function setScore

//function to calculate score average
double getAverage(int score1,int score2,int score3){

int total=0;
	total=score1+score2+score3;

return static_cast<double>(total)/3; //Attention here to get decimal precision
//from calculate total/3 
	//you  have to convert -temporally- variable total of type int to double, 
	//and this is what this statement does. static_cast<double>(total)/3;
}//end function getAverage


//function definition for printOutput
void printOutput(int score1,int score2,int score3,double average){
	cout<<"\nScores:\n";
	cout<<"\nScore 1: "<<score1
		<<"\nScore 2: "<<score2
		<<"\nScore 3: "<<score3<<endl;
	cout<<"------------"
	<<"\nAverage: "<<average<<endl;
}//end function printOutput 
How do I do step 5 in step 3? I'm confused. It says to put before but in parentheses, it says after.
In the end:

Lastly, remove all the cout statements in the main (and may be in your functions)
that was put in for debugging purposes.

by the way i think that you are mixing 2 exercises 1 easier than next!
I am so sorry, I am still confused! I am also so sleepy but this is due in 3 hours. Yes, I ended in step 3, in which is the first step that I pasted.
This, how do I do this:

4) Add definition for your calcAverage() function by taking in the three values
passed and then calculate and return the value for average.
5) After the statement done in step 3 of this task (above) add a statement to
print the value for average returned from the calcAverage() function.
6) Run your program and enter three different scores and check that the average
is calculated (and displayed) correctly.
Do continue if this process is not working correctly.
7) Add documentation for your function; what it does, preconditions and
postconditions.
4) is done, its the definition of the function.
5)You delete this step in the end.
6)Compile and run your program.
7)This depends on your class.
Part 4.
1) Then add a prototype (before the main function) for a function called
printOutput() with the following prototype.
void printOutput(int score1,int score2,int score3,double average);
Note that prototype could also be like this
void printOutput(int ,int ,int ,double);
That is, only the data type is mandatory.
2) In your main function, add a statement (after work done for part 3 above)
that will call the printOutput() function. It will be like this.
printOutput(score1,score2,score3,average);
3) Add definition for your printOutput() function by printing the three scores
and also their averages.
4) Run your program and test that the average is displayed correctly. You still
should have the statement in main() that print the average, and this value
and the value displayed in the printOutput() function should agree.
Do continue if this process is not working correctly.
5) Add documentation for your function; what it does, preconditions and
postconditions.
1)done
2)done
3)done
4)done
5)This depends on your class -course

I need your help in something,
can allow private messages from your account settings?
What is left that I have to do in my code though?

#include <iostream>
using namespace std;

void printInfo();
int getScore ();
double calcAverage (int score1,int score2, int score3);
void printOutput (int,int,int,double);
int main() {
int score1 = 0, score2 = 0, score3 = 0;
double average;

printInfo ();
cout << endl << score1;
cout << score2;
cout << score3 << endl;
cout << endl << "Score 1 : ";
score1 = getScore();
cout << score1 <<endl;
cout << endl << "Score 2 : ";
score2 = getScore();
cout << score2 <<endl;
cout << endl << "Score 3 : ";
score3 = getScore();
cout << score3 <<endl;

average = calcAverage (score1, score2, score3);
printOutput (score1,score2,score3,average);

return 0;

}
/*
void printInfo ()
post: Information is posted on the Screen.
*/
void printInfo()
{
cout << "Enter three scores for three different exams.";
cout << "The program will calculate ";
cout << "\nthe average and then display the average.";
}
// Askes user to enter a value and gets score
int getScore ()
{
int score;
cout << "Enter a value: ";
cin >> score;
return score;
}
I'm comparing the code you posted to my uncompleted one and I still don't understand how the code came from the directions.
Thank you for the code but I want some understanding too. I don't get how you came up with the ending.
I did it step-by-step...maybe i can't figure a way to teach you better :/ i'm sorry its the best that i can do!
Looking at my original code, what can you tell that I should do next?
Just follow the instructions...these instructions are so simple, just need to know the basic concepts about functions prototypes,definitions, return types and parameter list, what is a calling function, and thats all!
Last edited on
//function definition for printOutput
void printOutput(int score1,int score2,int score3,double average){
cout<<"\nScores:\n";
cout<<"\nScore 1: "<<score1
<<"\nScore 2: "<<score2
<<"\nScore 3: "<<score3<<endl;
cout<<"------------"
<<"\nAverage: "<<average<<endl;
}//end function printOutput

What is that cout<<"-------------" supposed to say?

Scores:

Score 1: 4
Score 2: 19
Score 3: 32
------------    <---THIS LINE
Average: 18.3333

Pages: 12