// Chapter 10 Learning Task
//
// ADD STATEMENT 1 TO DECLARE FUNCTION PROTOTYPE FOR FUNCTION "low"
// THE FUNCTION DOES NOT RETURN DATA
// THE PARAMETER LIST CONTAINS A FLOAT POINTER AND AN INTEGER
void low(int *, int);
//
// ADD STATEMENT 2 TO DECLARE FUNCTION PROTOTYPE FOR FUNCTION "high"
// THE FUNCTION DOES NOT RETURN DATA
// THE PARAMETER LIST CONTAINS A FLOAT POINTER AND AN INTEGER
void high(int *, int);
//
// ADD STATEMENT 3 TO DECLARE FUNCTION PROTOTYPE FOR FUNCTION "averge"
// THE FUNCTION RETURNS A FLOAT VALUE
// THE PARAMETER LIST CONTAINS A FLOAT POINTER AND AN INTEGER
int average(int *, int);
//
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//
// declare variables
int numelements = 0; // number of storage locations in the array
//
// ADD STATEMENT 4 TO DECLARE A "float" POINTER VARIABLE NAMED "pointer"
int *pointer;
int avg = 0.0;
int exit = 0;
//
// output user information
cout << "\nChapter 10 Learning Task.\n\n";
cout << "\nThis program uses pointers to find the average of test"
<< "\nscores. It also finds the lowest and highest"
<< "\ntest score.\n\n";
//
//
// prompt user to enter number of scores used
cout << "\nHow many test scores will you enter? ";
cin >> numelements;
//
// validate data
while(numelements < 0)
{
cout << "\nAmount of test scores can not be negative!\n\n";
cout << "Enter another amount: ";
cin >> numelements;
}// end of while
//
// create dynamic array
//
// ADD STATEMENT 5 TO CREATE A "new" DYNAMIC ARRAY TO ASSIGN THE
// NUMBER "numelements" TO BE ADDED TO THE "pointer" VARIABLE
pointer = new int[numelements];
//
cout << "\n";
//
// use a for..loop to enter test scores
for(int x= 0; x < numelements; x++)
{
cout << "Enter test score " << x + 1 << " ";
cin >> *(pointer + x);
// validate data
while(*(pointer + x) < 0)
{
cout << "\nTest scores can not be negative!\n\n";
cout << "Enter another test score: ";
//
// ADD STATEMENT 6 TO INPUT A TEST SCORE
// USING THE "pointer" ARRAY
cin >> *pointer;
}// end of while
}// end of for
// output numbers entered
cout << "\nFOLLOWING ARE THE TEST SCORES\n";
for (int y = 0; y < numelements; y++)
{
cout << fixed << setprecision(1);
//
// ADD THE MISSING PART OF STATEMENT 7 TO OUTPUT THE SCORES IN THE
// ARRAY USING THE "pointer" ARRAY
cout << setw(6) << *pointer;
} // end of for
//
// call the average() to calculate average of the test score
avg = average(pointer, numelements);
//
// output the average of the test scores
cout << "\n\nTHE AVERAGE OF THE TEST SCORES IS: " << avg;
//
// call the function low() to find the lowest test score
low(pointer, numelements);
//
// call the function high() to find the highest test score
high(pointer, numelements);
//
// delete the pointer structure
delete [] pointer;
//
cout << "\n\n";
} // end of Chapter 10() function
//
// Following are all the Chapter 10() functions
// average()
int average(int* score, int numscores)
{
int total = 0.0;
//
// use a for loop to accumulate test scores
for(int x =0; x < numscores; x++)
//
// ADD THE MISSING PART OF STATEMENT 8 TO ACCUMULATE THE SCORES IN THE
// ARRAY USING THE "score" ARRAY
total = total + *score;
//
// calculate the average of the test scores
return total/numscores;
}// end of average
//
// low()
void low(int* score, int numelements)
{
int small = *(score + 0);
//
// use a for loop to find smallest value
for (int x = 0; x < numelements; x++)
{
if( *(score + x) < small)
{
// ADD MISSING PART OF STATEMENT 9 TO ASSIGN THE SMALLEST
// VALUE IN THE "score" ARRAY TO THE VARIABLE "small"
small = *score;
*(score + x) = *(score + (x + 1));
} // end if
} // end of for
//
// output smallest test score
cout << "\nTHE LOWEST TEST SCORE IS " << small;
}// end low()
//
// high()
void high(int* score, int numelements)
{
int large = *(score + 0);
//
// use a for loop to find smallest value
for (int x = 0; x < numelements; x++)
{
if( *(score + x) > large)
{
// ADD MISSING PART OF STATEMENT 10 TO ASSIGN THE LARGEST
// VALUE IN THE "score" ARRAY TO THE VARIABLE "large"
large = *score;
*(score + x) = *(score + (x + 1));
} // end if
} // end of for
//
// output smallest test score
cout << "\nTHE HIGHEST TEST SCORE IS " << large;
}// end high()
Compiles but I am not getting the right output.
Page 1 of 14 Replies - 132 Views - Last Post: 20 November 2012 - 02:44 AM
#1
Compiles but I am not getting the right output.
Posted 19 November 2012 - 12:53 AM
I can get it to compile but it outputs only the first number I entered. For instance I tell it 4 numbers, 95,75,85,and 90. It should output those numbers again, and then give me the average, the low and the high. Instead I just get 95 for all the scores, 95 for the average, low and high.
Replies To: Compiles but I am not getting the right output.
#2
Re: Compiles but I am not getting the right output.
Posted 19 November 2012 - 04:14 AM
//high() *(score + x) = *(score + (x + 1)); //low() *(score + x) = *(score + (x + 1));
What are these supposed to do?
//average() total = total + *score;
What does this do?
Also, your average() returns an int, are you sure about it? What is average value of {1, 2}?
#3
Re: Compiles but I am not getting the right output.
Posted 19 November 2012 - 09:03 PM
The low is supposed to find the lowest score, the high is supposed to find the highest and average finds the average.
#4
Re: Compiles but I am not getting the right output.
Posted 20 November 2012 - 02:17 AM
I'm not asking what your functions are supposed to do. That much if obvious, and if everything was alright, you wouldn't be here. 
I'm asking what these particular lines do. Look at them, and tell me what happens in these lines, step by step. Then tell me why these lines are where they are, what is their purpose in context of algorithms they are parts of.
Really, I'm not just saying that to feel smarter than you, I'm trying to show you where the errors lie, but for you to get something out of it, you have to have your own "Eureka!" moment. Just look at these lines and answer my questions.
I'm asking what these particular lines do. Look at them, and tell me what happens in these lines, step by step. Then tell me why these lines are where they are, what is their purpose in context of algorithms they are parts of.
Really, I'm not just saying that to feel smarter than you, I'm trying to show you where the errors lie, but for you to get something out of it, you have to have your own "Eureka!" moment. Just look at these lines and answer my questions.
#5
Re: Compiles but I am not getting the right output.
Posted 20 November 2012 - 02:44 AM
Ok, let me help you out in one of Xupicor's questions where he asked
1.Think of any value for 'total' and any value for 'numscores'
When you divide these 2 numbers, are you getting a whole number?
If yes,click on the spoiler tag.
If no,click on the spoiler tag below.
regards,
Raghav
Quote
Also, your average() returns an int, are you sure about it?
1.Think of any value for 'total' and any value for 'numscores'
When you divide these 2 numbers, are you getting a whole number?
If yes,click on the spoiler tag.
Spoiler
If no,click on the spoiler tag below.
Spoiler
regards,
Raghav
This post has been edited by raghav.naganathan: 20 November 2012 - 02:46 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|