Question

In: Computer Science

Write a C program that uses a loop which prompts a user for an integer 10...

Write a C program that uses a loop which prompts a user for an integer 10 times and stores those integers in an array. The program will print out the numbers entered by the user with a comma and a space separating each number so the numbers can be read easily. The program will then calculate and print out the sum, integer average and floating point average (to 4 decimal points) of the numbers in the array. The information being printed out should be labeled correctly with newline characters placed so the output is readable. You will also create a BubbleSort function to sort the integers from small to large. The array will be passed by reference to the function where the function will reorder the integers in the array. You should not duplicate the array in anyway.

Remember your assignment must be submitted by 11:59:59pm on the due date, or else it will be considered late and will not be accepted. Please make sure your C code is thoroughly commented to explain what your program is doing. You should only submit the C source code, not the compiled program.

NOTE: Pseudo-code for a basic Bubble Sort routine:

procedure BubbleSort( A : array of integers to sort)
  repeat
    swapped = false
      for item in A inclusive do:
        if A[i-1] > A[i] then
          swap A[i-1] and A[i]
          swapped = true
        end if
      end for
  until not swapped
end procedure

Sample Output:

$>./assignment3
Enter element  1 into the array 5
Enter element  2 into the array 7
Enter element  3 into the array 6
Enter element  4 into the array 1
Enter element  5 into the array 2
Enter element  6 into the array 3
Enter element  7 into the array 4
Enter element  8 into the array 8
Enter element  9 into the array 9
Enter element 10 into the array 0

5, 7, 6, 1, 2, 3, 4, 8, 9, 0

The sum of the integers in the array is 45
The integer average of the integers in the array is 4
The float average of the integers in the array is 4.5000

0, 1, 2, 3, 4, 5, 6, 7, 8, 9
$>

Solutions

Expert Solution

C Program:

#include <stdio.h>

void swap(int *a, int *b){

int temp;

temp = *a;

*a = *b;

*b = temp;

}

void bubbleSort(int * const arr, const int size){

int i, j=0;

for(i=0;i<size-1;i++){

for(j=0;j<size-1;j++){

if(arr[j] >arr[j+1]){

swap(&arr[j],&arr[j+1]);

}

}

}

}

int main() {

int arr[10];

int sum,avg;

float avg1;

for(int i=0;i<10;i++){

printf("Enter element %d into the array: ",i+1);

scanf("%d",&arr[i]);

sum+=arr[i];

}

for(int i=0;i<10;i++){

if(i!=9)

printf("%d, ",arr[i]);

else

printf("%d\n",arr[i]);

}

bubbleSort(arr,10);

avg=sum/10;

avg1=(double)sum/10;

printf("The sum of integers in the array is %d\n",sum);

printf("The integer average of the integers in the array is %d\n",avg);

printf("The integer average of the integers in the array is %f\n",avg1);

for(int i=0;i<10;i++){

if(i!=9)

printf("%d, ",arr[i]);

else

printf("%d",arr[i]);

}

}

if you like the answer please provide a thumbs up.


Related Solutions

Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Write a program that uses a loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT