You are here:

- Home
- Computing/Technology
- C/C++
- C++
- What am I doing wrong with my program?

Advertisement

where the calcResults()is, I am trying to write the code that will prompt the user to enter the specified number of values indicated in the function's parameter and get that number of floating point values from the user, calculate sum, average, and median.

In the main() function, at the location indicated in the comment, you are to place the proper calls to the getIntRange() and calcResults() to accept a second series of float values.

The sample output is supposed to look like this.

Enter an integer in the range 3 to 6: 12

Number out of range. Try again.

Enter an integer in the range 3 to 6: -2

Number out of range. Try again.

Enter an integer in the range 3 to 6: 4

Enter number 1: 10

Enter number 2: 100

Enter number 3: 4

Enter number 4: 3

The sum of the values was 117

The average was 29.25

The median was 51.5

Enter an integer in the range 5 to 10: 2

Number out of range. Try again.

Enter an integer in the range 5 to 10: 10

Enter number 1: 1.2

Enter number 2: 1.01

Enter number 3: 1.1

Enter number 4: 1.5

Enter number 5: 1.12

Enter number 6: 1.4

Enter number 7: 1.6

Enter number 8: 10.0

Enter number 9: 1.1

Enter number 10: 1.11

The sum of the values was 21.14

The average was 2.114

The median was 5.505

I tried to write a code for this program and I have this.

int getIntRange (int minValue, int maxValue) {

int numEntered, error=1;

do {

cout << "Enter an integer in the range " << minValue << " to "

<< maxValue << ": ";

cin >> numEntered;

if (numEntered < minValue || numEntered > maxValue)

cout << "Number out of range. Try again." << endl;

else error = 0;

} while (error == 1);

return numEntered;

}

int calcResults (int numValuesEntered) {

int sum = 0, avg, median, max, min, numIn, integer;

if(numIn % 1 == 0)

numIn = integer;

numValuesEntered = 1;

while(numIn = integer) {

cout << "Enter number " << numValuesEntered << ": ";

cin >> numIn;

sum+=numIn;

return sum;

if(numValuesEntered == 1)

min = max = numIn;

else if(numIn < min)

min = numIn;

else if(numIn > max)

max = numIn;

numValuesEntered++;

}

avg = sum / numValuesEntered;

return avg;

median = (max + min)/2;

return median;

cout << "The sum is " << sum << endl;

cout << "The average is " << avg << endl;

cout << "The median is " << median << endl;

}

int main() {

int num, num2;

num = getIntRange(3,6);

calcResults (num);

num = getIntRange(5,10);

calcResults (num2);

return 0;

}

It compiles ok, but the output is totally out of whack.

I am really confused about, how I am going to separate the numbers like in the sample output. I am stumped on this program, and I have no idea what I am doing wrong.

I'd appreciate it very much, if you can give me few pointers about what I am doing wrong.

Thank you.

You seem to have started programming in this language recently.

There are several bugs in your code. I've corrected them for you. You'll need to compile them.

You're returning from within the calcResults function several times. No subsequent lines after return will be executed.

void calcResults (int numValuesEntered) {

int sum = 0, avg, median, max, min, numIn = 1, integer;

while(numIn <= numValuesEntered) {

cout << "Enter number " << numValuesEntered << ": ";

cin >> numIn;

sum+=numIn;

numIn++;

}

avg = sum / numValuesEntered;

median = (max + min)/2;

cout << "The sum is " << sum << endl;

cout << "The average is " << avg << endl;

cout << "The median is " << median << endl;

}

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | very good. Thank you. It's fun to learn c , but not always easy. you've been very helpful |

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Programming in CPP for about 8 years. Industry experience of 4 years. **Education/Credentials**

Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.