Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15054 > unrolled thread

Will someone tell me how to handle only input between -1 and 100.?

Started bynaushil mehta <naushil9@gmail.com>
First post2012-03-25 12:13 -0700
Last post2012-03-26 11:33 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Will someone tell me how to handle only input between -1 and 100.? naushil mehta <naushil9@gmail.com> - 2012-03-25 12:13 -0700
    Re: Will someone tell me how to handle only input between -1 and         100.? Sam <sam@email-scan.com> - 2012-03-25 16:35 -0500
    Re: Will someone tell me how to handle only input between -1 and 100.? gwowen <gwowen@gmail.com> - 2012-03-26 03:31 -0700
    Re: Will someone tell me how to handle only input between -1 and 100.? Christopher Pisz <someone@somewhere.net> - 2012-03-26 11:33 -0500

#15054 — Will someone tell me how to handle only input between -1 and 100.?

Fromnaushil mehta <naushil9@gmail.com>
Date2012-03-25 12:13 -0700
SubjectWill someone tell me how to handle only input between -1 and 100.?
Message-ID<12850768.0.1332702808912.JavaMail.geo-discussion-forums@pbdl8>
This is the comment from my instructor,thanks

-1 pt  for readArray() handles the out of range data (input <-1) as well as (input >100) the same valid data!
when program encounters the out of range data ( input <-1 || input >100), program prompt user “wrong input, and enter again”. Do not enter the out of range (invalid data) to the score[] array.

[code]


-1 pt  for readArray() handles the out of range data (input <-1) as well as (input >100) the same valid data!
when program encounters the out of range data ( input <-1 || input >100), program prompt user “wrong input, and enter 
again”. Do not enter the out of range (invalid data) to the score[] array.
// Lab 4
// Programmer: Naushil Mehta
// Editor(s) used: win 7 notepad
// Compiler(s) used: Visual Studio 2008
//

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

int readArray(int, int[]);//implement step 2 of 5

int avg(int , const int[]); //implement step 3 of 5

int stat(int, const int[], int&, int&, int&); //implement step 4 of 5

int histogram(int,const int[], int[]);//implement step 5 of 5

int main()
{
	const int MAX_SCORES = 100;
	int score[MAX_SCORES];
	//cout<< readArray(MAX_SCORES,score)<<endl;
	int nScore = readArray(MAX_SCORES,score);
	cout<<"Average of the array: "<<avg(nScore, score) <<endl;
	//step 4 of 5
	int avgScore, minScore, maxScore;
	
	int grade[5]={0};

	if (stat( nScore, score, avgScore, minScore, maxScore) == 0)
	{
		cout<<"Average = "<< avgScore << endl
			<<"Max = "<<maxScore <<endl
			<<"Min = "<<minScore <<endl;
		//step 5 of 5
		histogram(nScore,score,grade);
		
		cout <<"As: "<< grade[0]<<endl;
		cout <<"Bs: "<< grade[1]<<endl;
		cout <<"Cs: "<< grade[2]<<endl;
		cout <<"Ds: "<< grade[3]<<endl;
		cout <<"Fs: "<< grade[4]<<endl;
	}
	else
	{
		cout << "no data"<<endl;
	}
	cin.get();
	cin.get();
    return 0;
}

int readArray(int max_score, int score[])
{
	int nScores =0;
	for(int i =0; i< max_score; i++)
	{
		cout<<"Enter element(-1 to end input): ";
		cin >> score[i];
		cout<<endl;

		if (score[i] == -1)
		{
			score[i] = NULL;
			break;
		}
		else
		{
			nScores++;
		}
		if(nScores>=max_score)
			break;

		cin.ignore();
	}
	return nScores;
}
int avg(int n, const int score[])
{
	int total = 0;
	if (n == 0)
		return 0;

	for(int i = 0; i < n; i++)
	{
		total += score[i];
	}
	return total/n;
}
int stat(int n, const int score[], int& avg, int& mn, int& mx)
{
	if (n == 0)
		return 1;
	int total = 0;
	for(int i = 0; i < n; i++)
	{
		total =total + score[i];
		if (( i == 0) || (mn > score[i]))
			mn = score[i];
		if ((i == 0) || (mx < score[i]) )
			mx = score[i];
	}
	avg = total / n;
	return 0;

}
int histogram(int nScore,const int score[], int grade[])
{
	if(sizeof(score)/sizeof(int)==0)
		return 1;
	for(int i = 0; i < nScore; i++)
	{
		if (score[i] >= 90)
		{
			grade[0]++;
		}
		else if(score[i] >= 80)
		{
			grade[1] ++;
		}
		else if(score[i] >= 70)
		{
			grade[2] ++;
		}
		else if(score[i] >= 60)
		{
			grade[3] ++;
		}
		else
		{
			grade[4] ++;
		}
	}
	return 0;
}
[/code]

[toc] | [next] | [standalone]


#15062 — Re: Will someone tell me how to handle only input between -1 and 100.?

FromSam <sam@email-scan.com>
Date2012-03-25 16:35 -0500
SubjectRe: Will someone tell me how to handle only input between -1 and 100.?
Message-ID<cone.1332711325.25141.29721.1000@monster.email-scan.com>
In reply to#15054

[Multipart message — attachments visible in raw view] — view raw

naushil mehta writes:

> This is the comment from my instructor,thanks

No, we will not do your homework for you.


[toc] | [prev] | [next] | [standalone]


#15065

Fromgwowen <gwowen@gmail.com>
Date2012-03-26 03:31 -0700
Message-ID<46ba911a-ba2a-436a-aaf4-7efa014f0d62@i2g2000vbv.googlegroups.com>
In reply to#15054
I'm not going to do you homework for you, either... but here are a few
hints on how to think about the problem.

> int readArray(int max_score, int score[])
> {
>         int nScores =0;
>         for(int i =0; i< max_score; i++)
>         {
>                 cout<<"Enter element(-1 to end input): ";
>                 cin >> score[i];
>                 cout<<endl;

Here, you read straight into the array - but if the input is invalid
you don't want to store it in the array, do you?

Perhaps you could store it somewhere else temporarily, and only put it
in the array once you've determined if it is valid.

>                 if (score[i] == -1)
>                 {
>                         score[i] = NULL;

NULL is a pointer value. You don't want to store a pointer value as if
it were an integer.

>                         break;
>                 }
>                 else
>                 {
>                         nScores++;

Here you're incrementing the number of valid scores you've stored.
Looks like a good place to check the validity of the score before
storing it.

[toc] | [prev] | [next] | [standalone]


#15067

FromChristopher Pisz <someone@somewhere.net>
Date2012-03-26 11:33 -0500
Message-ID<jkq5o5$66o$1@dont-email.me>
In reply to#15054
On 3/25/2012 2:13 PM, naushil mehta wrote:
> This is the comment from my instructor,thanks

You've already posted your homework assignment 4 times in separate 
threads. You would probably get more help if you:

1) Posted one thread for one topic
2) Gave the assignment your best shot yourself
3) Asked a _specific_ question about what you are having trouble with 
after #2

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web