Question

Write a C program to do the following 1) request user to enter 10 integer into...

Write a C program to do the following
1) request user to enter 10 integer into an array

2) sort the array in ascending order

3) display the sorted array

4) count the number of odd and even number in the array and print out the result

5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result

6) write function addNumber() to add all the number in the array. return the value to main() and prints the result

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Program:

Output Snapshot:

Text Code:

#include <stdio.h>
// Declaration of the function
int addNumber(int arr[], int n);

int main()
{
int arr[10], i, j, oddCount=0, evenCount=0, oddNumAvg, evenNumAvg;
  
// Request user to enter 10 integer into an array
printf("\n========= User Input =========\n");
for(i=0; i<10; i++){
printf("Enter arr[%d] :",i);
scanf("%d", &arr[i]);
}

// Sort the array in ascending order (using Bubble Sort)
for (i=0; i< 9; i++){
for (j = 0; j < 10-i-1; j++){
if (arr[j] > arr[j+1]){
// Swapping the arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
  
// Displaying the sorted array
printf("========= Sorted Array =========");
for(i=0; i<10; i++){
printf("\narr[%d] : %d",i, arr[i]);
}
  
// count the number of odd and even number in the array and print out the result
printf("\n===== Even/Odd Number Count =====");
for(i=0; i<10; i++){
if(arr[i]%2==0){ // When array element is even
evenCount++;
} else { // When array element is odd
oddCount++;
}
}
printf("\nEven Number Count : %d", evenCount);
printf("\nOdd Number Count : %d", oddCount);
  
// Add the value of the odd number and even number and calculate the average
// of the odd and even number. display the result
printf("\n===== Sum of Even/Odd Number =====");
int sumEven = 0, sumOdd = 0;
for(i=0; i<10; i++){
if(arr[i]%2==0){
sumEven = sumEven + arr[i];
} else {
sumOdd = sumOdd + arr[i];
}
}
printf("\nEven Number Average : %f", ((float)sumEven/evenCount));
printf("\nOdd Number Average : %f", ((float)sumOdd/oddCount));
  
// Add all the numbers in the array and return the sum
printf("\n===== Sum of All Numberin Array =====");
printf("\nSum of all the numbers : %d", addNumber(arr,10));
return 0;
}

// User-defined function to add all the array elements and return its summation
int addNumber(int arr[], int n){
int i, sum = 0;
for(i=0; i<n; i++){
sum = sum + arr[i];
}
return sum;
}

Explanation:

  • To sort the array in ascending order, Bubble Sort has used in the code, it is the simplest of all the sorting algorithm, which compares the adjacent element and swap if previous element is larger than the next element.
  • The variable used in the code are as per the its use.
  • In each step, there is separate for loops are used for the simplicity and understandability of the code.
  • Please refer code comments for the understanding purpose.
Add a comment
Know the answer?
Add Answer to:
Write a C program to do the following 1) request user to enter 10 integer into...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2...

    Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2 so that it is modular. Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a subroutine called print_array that accepts an integer array and prints it out in this format: The array is [01 2 3456 7 8 9] Use this function...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT