Question

Write a C program Design a program that uses an array to store 10 randomly generated...

Write a C program

Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user:

  1. Display 10 random numbers stored in the array
  2. Compute and display the largest number in the array
  3. Compute and display the average value of all numbers
  4. Exit

The options 2 and 3 should call an appropriate user-defined function and pass the array to the function to compute the largest and the average value respectively. Design and call these two user-defined functions. The average value should be calculated and displayed with a precision of two decimal places.

The program should loop back to the main menu until the user selects the option to exit the program.

Use the register storage class for two the most frequently used variables in your program.

Submit your program's source code (i.e., .c file) and a file with screen captures of your program's

Homework Answers

Answer #1

Implement the program as follows:

  1. define the function largest() that accepts numbers array
  2. initialize largest as first element
  3. for each element in array
  4. update largest if the element > largest
  5. return largest
  6. define the function average() that accepts numbers array
  7. initialize sum to 0
  8. for each element in array
  9. add it to sum
  10. calculate average as sum/10 and return average
  11. define main() function
  12. declare integer variables
  13. declare register storage variables using the keyword, register
  14. initialize random module
  15. generate 10 random numbers and store it in numbers array
  16. limit generated random numbers in range 1-50
  17. Display menu options
  18. ask the user to enter an input
  19. read input
  20. store input value in choice
  21. display all the numbers if choice = 1
  22. display largest using largest() if choice = 2
  23. display average using average() if choice = 3
  24. repeat until choice!= 4

Program:

#include <stdio.h>
#include <stdlib.h>

int largest(int numbers[]){                                             /* define the function largest() that accepts numbers array */
    int largest=numbers[0], j;                                          /* initialize largest as first element */
    for(j = 0; j<10;j++){                                               /* for each element in array */
        if(numbers[j]>largest){                                         /* update largest if the element > largest */
            largest = numbers[j];
        }
    }
    return largest;                                                     /* return largest */
}


double average(int numbers[]){                                          /* define the function average() that accepts numbers array */
    int sum = 0, j;                                                     /* initialize sum to 0 */
    for(j = 0; j<10;j++){                                               /* for each element in array */
        sum = sum + numbers[j];                                         /* add it to sum */
    }
    return sum/10.0;                                                    /* calculate average as sum/10 and return average */
}


int main()                                                              /* define main() function */
{
    int numbers[10], input;                                             /* declare integer variables */
    register int i, choice;                                             /* declare register storage variables using the keyword, register */
    srand(0);                                                           /* initialize random module */
    for(i=0; i<10;i++){                                                 /* generate 10 random numbers and store it in numbers array */
        numbers[i] = rand()%50 + 1;                                     /* limit generated random numbers in range 1-50 */
    }
    
    do{                                                                         /* Display menu options */
        printf("\n1. Display 10 random numbers stored in the array.");
        printf("\n2. Compute and display the largest number in the array.");
        printf("\n3. Compute and display the average value of all numbers.");
        printf("\n4. Exit");
        printf("\nPlease enter an option : ");                                  /* ask the user to enter an input */
        scanf("%d", &input);                                                    /* read input */
        choice = input;                                                         /* store input value in choice */
        switch(choice){
            case 1: printf("\nNumbers are: ");                                  /* display all the numbers if choice = 1 */
                    for(i=0; i<10;i++){
                        printf("%d ",numbers[i]);
                    }
                    break;
            case 2: printf("\nLargest is : %d", largest(numbers));              /* display largest using largest() if choice = 2 */
                    break;
            case 3: printf("\nAverage is : %.2f", average(numbers));            /* display average using average() if choice = 3 */
                    break;
        }
    }while(choice!=4);                                                          /* repeat until choice!= 4 */

    return 0;
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
DESIGN A FLOWCHART IN FLOWGORITHM Number Analysis Program Design a program that asks the user to...
DESIGN A FLOWCHART IN FLOWGORITHM Number Analysis Program Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display the following data: The lowest number in the array. The highest number in the array. The total of the numbers in the array. The average of the numbers in the array. PLEASE AND THANK YOU
DESIGN A FLOWCHART IN FLOWGORITHM STEP BY STEP Number Analysis Program Design a program that asks...
DESIGN A FLOWCHART IN FLOWGORITHM STEP BY STEP Number Analysis Program Design a program that asks the user to enter a maximum of 20 numbers. The program should store the numbers in an array and then display the following data: -The lowest number in the array. -The highest number in the array. -The total of the numbers in the array. -The average of the numbers in the array. PLEASE AND THANK YOU
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that...
DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that asks the user to enter a maximum of 20 numbers. The program should store the numbers in an array and then display the following data: 1-The lowest number in the array. 2-The highest number in the array. 3-The total of the numbers in the array. 4-The average of the numbers in the array. PLEASE AND THANK YOU
Design the logic for a program that allows a user to enter 10 numbers, stores the...
Design the logic for a program that allows a user to enter 10 numbers, stores the numbers in an array, then displays all of the numbers, the largest number, and the smallest.  create a solution algorithm using pseudocode  create a flowchart Need to do flowchart in Raptor and can't get it to do min and max or display the array
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
Write a program which: Write a program which uses the following arrays: empID: An array of...
Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary....
Design a C++ program where the user inputs the size of an array at the time...
Design a C++ program where the user inputs the size of an array at the time of execution with random numbers in the array. Then take every other odd number of the array and get the mean. Use delete and new functions.