11 Replies - 7303 Views - Last Post: 19 July 2012 - 11:37 AM Rate Topic: -----

#1 Daragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-July 12

Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 17 July 2012 - 10:06 AM

Hello, being new here, I hope I am following the correct format. Now then, as for my question.

Assignment

My assignment is to write a program that dynamically allocates an array large enough to hold a user defined number of test scores. Once all the scores are entered, the array should be passed to a function that finds the highest score. The array should be passed to a function that finds the lowest score. Lastly, the array should be passed to another function that calculates the average of all of the score.

As far as output goes, the program should show the highest, lowest and average of all of the scores.

Requirements:

Function average:

This function calculates and returns the
average of the values stored in the array
passed into the scores parameter. The
parameter numScores holds the number of
elements in the array.


The program must use pointer notation in the functions to pass the array. Also there needs to be user input protection to prevent negative numbers.

Required prototypes:

double average(double* score, int numScores);
double highest(double* score, int numScores);
double lowest(double* score, int numScores);

Ok, I barely understand a lot of this, I recently started this course and it is the first computer programing class I have ever taken. I need help understanding it. I also need help cleaning up my code.

Here is what I have so far:

#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;

template <class T>
T numScores(T min, T max);
void getScores(double* score, int numScores);
double average(double* score, int numScores);
double highest(double* score, int numScores); 
double lowest(double* score, int numScores);


int main()
{
    cout << "\nHow many test scores do you want to enter  : ";
    int number_of_tests = numScores<int>(0, 100);

    double* scores = new double[number_of_tests];

    getScores(scores, number_of_tests);

    cout << "The average of all " << number_of_tests << " test scores is: "
         << fixed << showpoint << setprecision(2)
         << average(scores, number_of_tests)
		 << "\n";

    delete [] scores;

    scores = NULL;

	system("pause");
	return 0;
}

template <class T>
T numScores(T min, T max)
{
    T num;
    for(;;)/>
    {
        cin >> num;
        
        if( cin.good() ) 
        {
            cin.sync();
            if(num >= 0)
                return num;
        }
        else
        {
            cin.clear(); 
            cin.sync();
        }
        cout << "Negative scores are not allowed.";
    }
}

void getScores(double values[], int numValues)
{
    cout << "Please enter the score for each test ...\n";
    for(int count=0; count<numValues; ++count)
    {
        values[count] = numScores<double>( 0, 100.0);
    }
}

double average(double score[], int numScores)
{
    if(numScores==0) return 0;

    double total = 0.0;

    for(int i=0; i<numScores; ++i)
        total += score[i];

    return total/numScores;
}




First off I used "template" function for the first time in this program. Honestly I have little understanding of how it works. If at all possible I want to rid myslef of this function until I am able to fully comprehend how to utilize it.

Errors:

None so far my program works fine, but it doesn't do what I want it to, I only want guidance on how to achieve what the question is asking me to.

Due date:

The assignment is due tomorrow, it is an online course and I have asked my teacher for help but he wasn't very helpful. I don't want to be a burden but if anyone can help me I need assistance as soon as possible.


Thank you in advance, this website has helped me a lot up until now, it is because of this that I decided to make an account. My teacher modified the "test scores" question which is why I can't seem to find this version anywhere on the web.

- Daragon, The Apprentice of Death...

Is This A Good Question/Topic? 0
  • +

Replies To: Test Scores Modified Finding Smallest and Largest Number in an Array

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,248
  • Joined: 05-May 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 17 July 2012 - 10:23 AM

It looks like you have code that computes the average. Good job!

Now you need code to implement highest() and lowest() and make calls to them the same way you are calling average().

As for the template function, if you don't like it or don't understand it, don't use it. In fact, the code there only checks for non-negative. It doesn't really even compare against min and max.

Just create two functions: getInt() and getDouble():
int getInt();
double getDouble();



Use getInt() to get the number of scores, obviously getDouble() to get the scores.

This post has been edited by Skydiver: 17 July 2012 - 10:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,678
  • Joined: 25-December 09

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 17 July 2012 - 10:26 AM

Quote

Ok, I barely understand a lot of this, I recently started this course and it is the first computer programing class I have ever taken. I need help understanding it. I also need help cleaning up my code.

Quote

First off I used "template" function for the first time in this program. Honestly I have little understanding of how it works. If at all possible I want to rid myslef of this function until I am able to fully comprehend how to utilize it.


I suggest you find another source to copy. If this is your first computer programming class there is no way you wrote this template function.

Quote

Errors:

None so far my program works fine, but it doesn't do what I want it to, I only want guidance on how to achieve what the question is asking me to.

If the program doesn't do what it is intended to do then there are errors.

Quote

My teacher modified the "test scores" question which is why I can't seem to find this version anywhere on the web.

Excellent, a teacher trying to insure you do your own work. I suggest you start over.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 Daragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-July 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 18 July 2012 - 12:55 PM

First off I would like to apologize!

well Jim I have to say that was a good kick in the teeth, but well deserved. I got to say you woke me up, I am in the process of re-reading my book given to me for my online course and comprehending it. This is my second attempt at the program. If I haven't offended you all too much for my stupid and childish mistake of trying to rip someone else off for their well written code, in a sad attempt to plagiarize, I would very much appreciate it if you could critique my work.

All of this is written by me other than the prototypes give to me by my teacher and they are a requirement.

#include <sstream>
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;

// Prototypes
void scores(double* score);
int tests();
double average(double* score, int numScores); 
double highest(double* score, int numScores); 
double lowest(double* score, int numScores);

int main()
{
	int numTests;
	// Funtion call tests
	numTests = tests();
	// Dynamically allocated array
	double* scores = new double [numTests];
	// Function call scores
	void scores(double* score[]);

system("pause");
return 0;
}

// Gets the scores
void scores(double score[])
{
	score;
	double tempScores;
	cout << "Enter a number for each test score:\n";
	for (int i = 0; i< score.size(); i++)
	{
		int temp;

		cin >> temp;

		if (temp >= 0) 
		{
			tempScores += temp;
		}
		else {
			cout << "Test scores can not be negative. ";
		}
	}
}

// Gets the number of tests
int tests()
{
	int numTests = 0;
	cout << "Enter the number of test scroes: ";
	while (true)
	{
		int temp;

		cin >> temp;

		if (temp >= 0) 
		{
			numTests += temp;
			return numTests;
		}
		else {
			cout << "The number can not be negative. ";
		}
	}
}

// Averages test scores
double average(double* score, int numScores)
{
    if(numScores==0) return 0;

    double total = 0.0;

    for(int i=0; i<numScores; ++i)
        total += score[i];

    return total/numScores;
}

//  Returns highest score
double highest(double* score, int numScores)
{

}

// Returns lowest score
double lowest(double* score, int numScores)
{

}




Here is my output when I try to run it:

1>------ Build started: Project: Lab07-02Proj 2, Configuration: Debug Win32 ------
1> TestScores.cpp
1>c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(22): error C2365: 'scores' : redefinition; previous definition was 'data variable'
1> c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(20) : see declaration of 'scores'
1>c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(34): error C2228: left of '.size' must have class/struct/union
1> type is 'double []'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

My question:

My question is this, how can I manipulate a pointer array in a function. I want to use .size to find the size of my dynamically allocated array. After which I want to put the user imputed numbers into that array. Any help would be appreciated.

Again I apologize there is no excuse for the way I acted it was just wrong.

- Daragon, The Apprentice of Death...
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,248
  • Joined: 05-May 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 18 July 2012 - 01:06 PM

Your line #22 is just another function prototype. You are not actually calling your scores() function.

As for using .size(). That will only work for the STL containers. You were probably reading about the std::vector<> which has a size() method the returns it's current size.

Since you have a raw array, you don't have the convenient method to call on your array. You'll have to pass in the size just like you did in your old code when computing the average. So your case, you'll need to add another parameter to your scores() function to take the array size.
Was This Post Helpful? 0
  • +
  • -

#6 Daragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-July 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 18 July 2012 - 01:40 PM

Alright, I'm kind of an idiot when it comes to code. I understand some of what you are saying I just don't know how to write it out. Line #22 I see what you are saying there but when I take void off of my function call I get these errors:

1>c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(22): error C2144: syntax error : 'double' should be preceded by ')'
1>c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(22): error C2064: term does not evaluate to a function taking 0 arguments
1>c:\users\timothy\desktop\tj cp107\cp107 projects\lab07-02sol 2\lab07-02proj 2\testscores.cpp(22): error C2059: syntax error : ')'

> You'll need to add another parameter to your scores() function to take the array size

I am currently searching the web on how to do this. Thanks for all of the help!

- Daragon, The Apprentice of Death...
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,248
  • Joined: 05-May 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 18 July 2012 - 01:51 PM

In your original post, how did you call average()?

Or even better, take time to read Jim's function tutorials. It's on his signature.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,678
  • Joined: 25-December 09

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 18 July 2012 - 03:04 PM

Quote

Or even better, take time to read Jim's function tutorials. It's on his signature.

Actually let's give credit to the proper author JackOfAllTrades, but they are indeed excellent function tutorials.

When you get errors that you don't understand post your current code along with the complete error messages exactly as they appear in your development environment and maybe we can help to explain the messages so they make sense to you. Reading an understanding the error messages is a very important skill to develop.

Also if you were given a link to study and don't understand something presented in the link show the parts of the link that you don't understand and ask specific questions based on the information presented. Since you seem to be willing to learn many of the regulars are happy to assist in any way we can. But remember we don't have deadlines, so be sure not to wait until the last minute to ask your questions.

I'm glad you came over to the light. :devil: You will find that by doing the work yourself you will learn soooo much more. One of the keys to programming is programming.


Jim

This post has been edited by jimblumberg: 18 July 2012 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 Daragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-July 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 19 July 2012 - 10:08 AM

Thank you both, I really must say this has been an eye opening experience :eek: I hope there is more to come.

Thanks to both of you I know have a nearly fully functional program. Now the only problem that I have is when I do enter a negative number, after the statement, "Scores can not be negative." The program displays the position of the array.

Here is my code:
I already passed the assignment in, but for future reference I would like to know why this happens.

#include <sstream>
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
// Prototypes
void getScores(double scores[], int numTests);
int tests();
double average(double scores[], int numTests); 
double highest(double scores[], int numTests); 
double lowest(double scores[], int numTests);

int main()
{
	int numTests;
	// Funtion call tests
	numTests = tests();
	// Dynamically allocated array
	double* scores = new double [numTests];
	// Function call scores
	getScores(scores, numTests);
	average(scores, numTests);
	highest(scores, numTests);
	lowest(scores, numTests);

	delete [] scores;
	system("pause");
	return 0;
}
// Gets the scores
void getScores(double scores[], int numTests)
{
	cout << "Enter a number for each test score:\n";
	// fills the array with scores
	for (int i = 0; i < numTests; i++)
	{
		cout << "Enter test score number " << i + 1 << " :" << endl;

		int temp;
		// test score imput
		cin >> temp;
		// test score imput goes into the array
		if (temp >= 0) 
		{
			scores[i] = temp;
		}
		else {
			cout << "Test scores can not be negative. " << i--;
		}
	}
}
// Gets the number of tests
int tests()
{
	int numTests = 0;
	cout << "Enter the number of test scroes: ";
	while (true)
	{
		int temp;
		cin >> temp;
		if (temp >= 0)
		{
			numTests += temp;
			return numTests;
		}
		else 
		{
			cout << "The number can not be negative. ";
		}
	}
}
// Averages test scores
double average(double scores[], int numTests)
{
	if(numTests == 0) return 0;
	double numScores = 0;
	
	for (int i = 0; i < numTests; i++)
	{
		int temp;
		temp = scores[i];
		numScores += temp;
	}
	double average = numScores/numTests;
	cout << "Average score: " << average << endl; 
}
//  Returns highest score
double highest(double scores[], int numTests)
{
	if(numTests == 0) return 0;

	double highestScore = 0;

	for (int i = 0; i < numTests; i++)
	{

			if(scores[i] > highestScore)
			{
				highestScore = scores[i];
			}
	}
	cout << "Highest score: " << highestScore << endl;
}

// Returns lowest score
double lowest(double scores[], int numTests)
{
	if(numTests == 0) return 0;

	double lowestScore = 0;
	
	for (int i = 0; i < numTests; i++)
	{
		if(i==0)
		{
			lowestScore = scores[i];
		}
		
		if(scores[i] < lowestScore)
		{
			lowestScore = scores[i];
		}
	}
	cout << "Lowest score: " << lowestScore << endl;
}




Any help will be appreciated as always.

- Daragon, The Apprentice of Death...
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,248
  • Joined: 05-May 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 19 July 2012 - 10:29 AM

Look closely at your line 48.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,678
  • Joined: 25-December 09

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 19 July 2012 - 10:30 AM

Look closely at this line?
cout << "Test scores can not be negative. " << i--;

In this line you are printing the value of i then decrementing it. If you don't want i to print then just decrement i on it's own line:
cout << "Test scores can not be negative. ";
i--;


Also you have some functions where you said you were going to return something, yet you don't return anything for all return paths. For example:
// Returns lowest score
double lowest(double scores[], int numTests)
{
	if(numTests == 0) 
      return 0;

	double lowestScore = 0;

	for (int i = 0; i < numTests; i++)
	{
		if(i==0)
		{
			lowestScore = scores[i];
		}

		if(scores[i] < lowestScore)
		{
			lowestScore = scores[i];
		}
	}
	cout << "Lowest score: " << lowestScore << endl;
}


In this function you say you are going to return a double to the calling function. In the first if you return 0, which will be implicitly converted to a double, when the condition is true. But if the condition is false, numTests > 0, you don't return anything. You should never lie to your compiler, if you tell the compiler you will return a value return a value. And that value should be of the correct type, don't rely on the compiler to properly convert your value to the correct type. So if you tell the compiler you are going to return a double then return a double (0.0) not an int (0) and insure all the function return paths return a value.

// Returns lowest score
double lowest(double scores[], int numTests)
{
	if(numTests == 0) 
      return 0.0;

	double lowestScore = 0;

	for (int i = 0; i < numTests; i++)
	{
		if(i==0)
		{
			lowestScore = scores[i];
		}

		if(scores[i] < lowestScore)
		{
			lowestScore = scores[i];
		}
	}
        // I would do this print out in main, after you returned the proper value.
	cout << "Lowest score: " << lowestScore << endl;
        return(lowestScore);
}



Also I recommend that you only have one statement per line:
if(test) return something; // Bad Practice IMO.
// instead do:
if(test)
   return something;


Jim
Was This Post Helpful? 0
  • +
  • -

#12 Daragon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-July 12

Re: Test Scores Modified Finding Smallest and Largest Number in an Array

Posted 19 July 2012 - 11:37 AM

Again you guys are great. Thank you so much I have learned a whole lot more on D.I.C than I would have on any "Do my homework for me." web site. I can't thank you enough Jim, I look forward to returning here very soon and I know that this community is a great place to be in.

- Daragon, The Apprentice of Death...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1