trouble with creating a loop to read certain number of inputs

This is a discussion on trouble with creating a loop to read certain number of inputs within the C++ Programming forums, part of the General Programming Boards category; hey everyone. i'm confused on how to create an array of numbers into a program and have that program ask ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    trouble with creating a loop to read certain number of inputs

    hey everyone. i'm confused on how to create an array of numbers into a program and have that program ask for each input. here is what i have to do:


    Write a program that reads 20 student homework scores. These score numbers can only be between 0 and 10. Use an array of integer type to store the scores. The program uses the scores to calculate the average score, the smallest score, and the highest score. The
    average score should have a decimal fraction. Then create three user-defined functions:
    - double average(int ar[])
    to calculate the average of the scores,
    - int smallest(int ar[])
    to find the smallest score and,
    - int largest(int ar[])
    to find the largest score.



    Function main

    Define an array of 20 integers
    Define an integer arrIndex and set to 0

    Use a loop to read 20 scores
    - each score is stored in an array element using arrIndex as the index.
    - increment the arrIndex

    Call the function average(), passing the array data, to get average of the numbers.
    Display the mean

    Call the function smallest(), passing the array data, to get smallest of the numbers.
    Display the smallest

    Call the function largest(), passing the array data, to get largest of the numbers.
    Display the largest number

    HINTS

    - Use #defined to define these constants for program
    #define ARRAY_SIZE 20
    #define HIGHEST_SCORE 10
    #define LOWEST_SCORE 0

    Then use ARRAY_SIZE in all the places that refer to the array size. Don’t hard-code the 20 inside your code!

    Use the ‘if’ statement to make sure that each entered score is between 0 and 10 inclusively.

    here is an example of an output:

    Enter the score 1: 10
    Enter the score 2: 9
    Enter the score 3: 11
    Invalid score! A score has to be from 1 up to 10.
    Enter the score 3: 9
    Enter the score 4: 8
    Enter the score 5: 1
    Enter the score 6: 3
    Enter the score 7: 7
    Enter the score 8: 6
    Enter the score 9: 6
    Enter the score 10: 8
    Enter the score 11: 5
    Enter the score 12: 4
    Enter the score 13: 6
    Enter the score 14: 7
    Enter the score 15: 8
    Enter the score 16: 9
    Enter the score 17: 4
    Enter the score 18: 4
    Enter the score 19: 7
    Enter the score 20: 7



    The average score is 6.40








    if someone could help i'd appreciate it very much. thank you!!

  2. #2
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    What have you tried to do? - Show any work you've done.
    Try a while loop that asks for the input.
    Code:
    #define ARRAY_SIZE 20
    int num;
    int array1[ARRAY_SIZE];
    int i = 0;
    	
    	
    cout << endl;
    cout << "Please enter 20 numbers between 0 and 10" << endl;
    	
    while(i < ARRAY_SIZE)
    {
    
    	cin >> num;
    
    	if(num > 10 || num < 0)
    	{
    		cout << endl;
    		cout << "Invalid score! A score has to be from 1 up to 10." << endl;
    	}
    	else
    	{
    		array1[i] = num;
    		cout << endl;
    		cout << "Enter the score " << i+1 << ": " << num;
    		cout << endl;
    		i++;
    	}
    }
    IDE - Visual Studio 2005
    Windows XP Pro

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating pthreads in for loop
    By 0624167 in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2008, 11:10 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Efficient way to read in a randum number of numbers
    By TrojanGekko in forum C++ Programming
    Replies: 0
    Last Post: 03-05-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21