Hey guys I was wondering if you could provide some help, It is pretty complex for a person who is just starting loops. I have included the problem as well as the code I have below. Please help me, I know their is something wrong with my for loop as well as logic and other things. I have included part of the rounding function.

Write a program to find the largest, smallest, and average values in a collection of numbers where the value of num will be the first data item read.

Please make the following addtions:
1. The user will enter the value for "num" where "num" is the number of values in
the data set to be entered. Give the user 3 attempts at entering a value for
" num " where "num" is in the range from 0 to 20, inclusive. If the number "num"
entered is out of range, write an error message and continue. If the user
does not enter a valid value for "num" within the 3 attempts print an error
message and terminate the program.

2. Include the rounding function you wrote earlier to round the Average
value to 3 decimal places.

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


double roundIt(double x, double n)  //Write definition for rounding function 3 decimal
{ 
    x = floor( x * pow(100.0, n) + 0.5) / pow(100.0, n);
    return x;
}


int main()                              
{
    
    int listNo, num, ct, large, small, sum;
    int att = 0;
    
    cout << "Enter number of numbers that you want to see in a list" << endl;
    cout << "that gives the average of. "
    cin >> listNo;
    
    
    do (att + 1)
    
    while ((listNo < 0 && listNo > 20) &&  att < 3)
    
    if( listNo >= 0 && listNo <= 20)
        if ( listNo != 0)
        {
          cout << " Enter enter first value on list: " << endl;
          cin >> num;
          
          ct = listNo
          large = num;
          small = num;
          sum = num;  
          
          for (listNo = 1; listNo < ct; listNo = listNo += 1)
          {
             cout << "Enter next value in list: " << endl;
             cin >> num;
             
             if ( x > large)
                large = x;
                
             if ( x < small)
               { small = x;
               sum = sum + x; }
          }
          
          cout <<    
             
             
             
             
    
   
    system ("pause");
    
    
    return 0;                            
}

Recommended Answers

All 5 Replies

First of all, you're not using any brackets ({ and }) with your loops. Note that the code for a while loop is

while (condition) {
     code goes here
}

The same goes for any conditional statement or loop where it executes more than one statement.
Secondly, you didn't set up the counter correctly. For int att, which is clearly the amount of attempts, initialize it at zero. Then, in your loop, include the line

att++;

Put this where you want to increment it, which will probably be at the end of the loop. This will increase it by one every time the code is run. And even though you can use a do...while loop here, it's not necessary. Your code is guaranteed to run at least once, because int att is initialized as zero, so at the beginning, that number is less than 3.
Also, for the conditional statements for listNo, you can eliminate the second one by just making the first one include listNo > 0, instead of >=.

Those are a few suggestions. If you want, you can make the changes and then post again.

commented: helpful!!! +1

Okay, so I've done a few of the things I mentioned:

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

double roundIt(double x, double n) { 
    x = floor( x * pow(100.0, n) + 0.5) / pow(100.0, n);
    return x;
}

int main(){
    
	int listNo, num, ct, large, small, sum;
	int att = 0;
    
    cout << "Enter number of numbers that you want to see in a list" << endl;
    cout << "that gives the average of. ";
    cin >> listNo;
    
    while ((listNo < 0 && listNo > 20) &&  att < 3) {
    
    	if( listNo > 0 && listNo <= 20) {
		cout << " Enter enter first value on list: " << endl;
		cin >> num;
          	} //is this end brace in the right place?

	ct = listNo;
	large = num;
	small = num;
	sum = num;  
          
	for (listNo = 1; listNo < ct; listNo = listNo++) { 
//note the use of incrementing in the for loop
		cout << "Enter next value in list: " << endl;
		cin >> num;
             
		if ( x > large)
			large = x;
                
		if ( x < small){ 
			small = x;
			sum = sum + x; 
			}
		}
	att++; //incrementing
	}
	cout << //what are you displaying here? the system pause?
	system ("pause"); //can also be cin.get(); (that waits for the user to press enter)
	return 0;
}

As you can see from the comment on line 26, code can become confusing when you forget the brackets...

att is for the attempts you give them at the beginning, in other words the start of the program asks them how many numbers in that list, i.e. where they want o see the greatest, least, and average. The att is the numbers of times it gives them to get it right. If they don't get that right in 3 attempts it won't show them the greatest, least and average, it will display an error message. e.g. "How many numbers do you want to see the greatest, least and average. But must be between 0-20?" If they input 6, then you enter those 6 numbers and it outputs the greatest. Instead if they enter 21 it will give them a second chance. If it is still not in range of 0-20 it will give them a 3rd chance and if they still don not do it, it dispalys something like "Sorry you were only given 3 chances to input a number for your list from 0-20 inclusive." I know it kinda sounds dumb but that is the way my instructor explained it to me, and that's what she wants. I need to rewrite the instructions. Whew!!!!

Start at the beginning. You know user will not be able to enter more than 20 numbers, but you don't know exactly how many until run time. One approach is to declare memory for 20 possible numbers and use only as many as indicated. A second approach is to wait until user inputs how many numbers they want to enter, and then use dynamic memory to declare an array of the exact size needed, or use dynamic memory to create a list of the exact size needed. With either of the first 2 approaches you will get all input first, then loop through to find the answers needed as output.

A third approach is to keep track data needed to output desired information at the end of the input as you go---if you will, on the fly calculations of smallest, largest, and average.

Your first task is to decide which approach you want to take.

Then use a loop to get user input between 0 and 20, inclusive. If it's zero, then stop the loop and exit the program I guess. If no number entered btween 0 and 20 within three attempts at input then exit the program as per instruction set. I wouldn't try do any more than that for now. When you can get all of that done, then, and only then go on to get the rest of the input and do the calculations.

commented: Helpful! +1

I finally got it! Here is the code below! Let me know what you think?

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



double roundIt(double x, double n)  //Write rounding function 3 decimal
{ 
    x = floor( x * pow(100.0, n) + 0.5) / pow(100.0, n);
    return x;
}


int main()  //start main                            
{
    
    float listNo, ct, large, small, sum;
    float num;
    int att = 0;
   
    
    do //do while input
    {
         cout << "Enter number of numbers that you want to see";
         cout << "in a list" << endl;
         cout << "that gives the largest, smallest, average of that is"; 
         cout << "between 0-20, inclusive. " << endl;
         cin >> listNo;
         att = att + 1;                          //3 attempts for correct value
    }  while ((listNo < 0 || listNo > 20) &&  att < 3); 
    
   
    
    if( listNo >= 0 && listNo <= 20) //in range
        
        if ( listNo != 0) //if not 0
        {
          cout << setprecision(4) << fixed << showpoint;
          cout << " Enter enter first value on list: " << endl;
          cin >> num;        
          
          ct = listNo;
          large = num;
          small = num;
          sum = num;  
          
          for (ct = 2; ct<= listNo; ct = ct + 1) //start of for loop
          {
             cout << "Enter next value in list: " << endl;
             cin >> num;
             
             if ( num > large)
                large = num;
                
             if ( num < small)
                small = num;
                
               sum = sum + num; 
           } //end of for loop 

                      //output of smallest number, largest, and average.
     cout << "\nThe largest number of the list is: " << large << endl;
     cout << "The smallest number is:  " << small << endl; 
     cout << "Average of list of numbers = " << roundIt(sum/listNo, 3) << endl; 
          }   //end of if not 0
    else                              //output if number for list is zero
        { cout << " \nYour list number is 0, cannot calculate smallest,";
        cout << "largest, average! " << endl; }
    
    else             //output if number for list is not in 0-20 range inclusive
        cout << "\nError! " << listNo << " is not in range! " << endl;

system ("pause");
    
    
    return 0;                            
}//end main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.