GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jan-2008, 15:41
herceca herceca is offline
New Member
 
Join Date: Dec 2007
Posts: 6
herceca is on a distinguished road

Finding and rranging numbers


I am supposed to find 7 numbers between 1-10 and then find the smallest(already done) and largest(already done) and then display the middle 5 numbers from lowest to highest in a line. I am currently stuck on this, I have tried many ways. Can someone help, please?



CPP / C++ / C Code:
# include <iostream.h>
# include <iomanip.h>
#include <ctype.h>
#include <stdlib.h>

using namespace std;

void getScores();
float findFirst(float,float,float,float,float,float,float);
float findLast(float,float,float,float,float,float,float);
float totalScore(float,float,float,float,float,float,float,float);

int main ()
	{
	void getScores();

	float score1,score2,score3,score4,score5,score6,score7,totalscore=0;
	float firstlowest=0;
	float lastlargest=0;
	float midonelowest, midtwo, midthree, midfour, midfive=0;
	
	cout<<"Enter the first score: ";
	cin >> score1;
		while (score1<0||score1>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score1;
			}
	cout <<"Enter the second score: ";
	cin >> score2;
		while (score2<0||score2>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score2;
			}
	cout <<"Enter the third score: ";
	cin >> score3;
		while (score3<0||score3>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score3;
			}	
	cout<<"Enter the fourth score: ";
	cin >> score4;
		while (score4<0||score4>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score4;
			}
	cout <<"Enter the fifth score: ";
	cin >> score5;
		while (score5<0||score5>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score5;
			}
	cout <<"Enter the sixth score: ";
	cin >> score6;
		while (score6<0||score6>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score6;
			}
	cout <<"Enter the seventh score: ";
	cin >> score7;
		while (score7<0||score7>10)
			{
				cout << "Wrong number, please try again: ";
				cin >> score7;
			}
	
    totalscore= totalScore(score1,score2,score3,score4,score5,score6,score7,totalscore);
	cout << "Your total score is "<<totalscore;
	firstlowest = findFirst(score1,score2,score4,score4,score5,score6,score7);
	cout << "\nYour lowest number is: " << firstlowest;
	lastlargest = findLast(score1,score2,score4,score4,score5,score6,score7);
	cout << "\nYour largest number is: " <<lastlargest<<endl;
	cin >> lastlargest; 
    return 0;
    }
    
    float totalScore(float num1,float num2,float num3,float num4,float num5,float num6, float num7, float totalscore)
    {
    
    totalscore = num1+num2+num3+num4+num5+num6+num7;
    
    return totalscore;
    }
    
	float findFirst(float mid1,float mid2,float mid3,float mid4,float mid5,float mid6, float mid7)
	{
	
	float firstlowest;
	firstlowest = mid1;
	
	if (mid2 < firstlowest)
		firstlowest=mid2;
	
	if (mid3 < firstlowest)
		firstlowest=mid3;
		
	if (mid4 < firstlowest)
		firstlowest=mid4;
	
	if (mid5 < firstlowest)
		firstlowest=mid5;	
	
	if (mid6 < firstlowest)
		firstlowest=mid6;
		
	if (mid7 < firstlowest)
		firstlowest=mid7;
	
	return firstlowest;
	}
	
	float findLast(float midd1,float midd2,float midd3,float midd4,float midd5,float midd6, float midd7)
	{
	
	float lastlargest;
	lastlargest = midd1;
	
	if (midd2 > lastlargest)
		lastlargest=midd2;
	
	if (midd3 > lastlargest)
		lastlargest=midd3;
		
	if (midd4 > lastlargest)
		lastlargest=midd4;
	
	if (midd5 > lastlargest)
		lastlargest=midd5;	
	
	if (midd6 > lastlargest)
		lastlargest=midd6;
		
	if (midd7 > lastlargest)
		lastlargest=midd7;
	
	return lastlargest;
	}
  #2  
Old 08-Jan-2008, 15:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Finding and rranging numbers


It seems like you could do all of that by simply putting them into an array and sorting the array. Have you ever used qsort()?
Maybe something like this:

CPP / C++ / C Code:
float scores[7];
int i;

for(i=0;i<7;i++)
{ // get user input and put scores into scores[i]
}

// use qsort to sort the array

// print results
cout << "\nYour lowest number is: " << scores[0];
cout << "\nYour highest number is: " << scores[6];
//etc.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.