Chapter 06 Program Exercises 1) PE 06 01 (Basic Arrays with Random Numbers) This
ID: 3908470 • Letter: C
Question
Chapter 06 Program Exercises 1) PE 06 01 (Basic Arrays with Random Numbers) This program requires the creation and use of four specific functions (max, min, totalOdds, and totalEvens). Write a program that loads two arrays with 10 random numbers that vary in value from 10 to 20. Use the functions you created to determine the largest, smallest, and total number of even and oda elements in each array. Your program should display each array to the screen followed by the largest value, smallest value, total even elements, and total odd elements. 2) PE 06_02 (Bubble Sorting Arrays) Prompt the user to enter a seed value (0 to 10) for the srandO function, then create a random array with 10 integers that range from 10 to 50. Use and modify the bubble sort routine discussed in class to sort the array (no optimization). Display the original random array to the screen. After each pass display the current pass number, modified array after each pass, and the total number of swaps for each pass. A sample format is illustrated below. Bonus (+5): Make this a sentinel-controlled progr Enter a seed number between 0 and 10: 1 Original: 10 27 30 24 32 31 49 12 35 38 pass: 1 swaps: 5 array: 10 27 24 30 31 32 12 35 38 49 pass: 2 swaps: 2 array10 24 27 30 31 12 32 35 38 49 pass: 3 swaps: 1 array: 10 24 27 30 12 31 32 35 38 49 pass: 4 swaps: 1 array 10 24 27 12 30 31 32 35 38 49 pass: 5 swaps: 1 array 10 24 12 27 30 31 32 35 38 49 pass: 6 swaps: 1 array: 10 12 24 27 30 31 32 35 38 49 pass: 7 swaps: 0 array 10 12 24 27 30 31 32 35 38 49 pass: 8 swaps: 0 array: 10 12 24 27 30 31 32 35 38 49 pass: 9 swaps: 0 array: 10 12 24 27 30 31 32 35 38 49 Enter a seed number between 0 and 10 (-1 to quit):Explanation / Answer
#include <stdio.h>
int main()
{
int i, num, upper=20, lower=10; //initialize lower & upper limit for random number generation
int arr1[5], arr2[5];
printf("Display Elements of Array 1 : ");
for (i = 0; i < 5; i++) {
num = (rand() % (upper - lower + 1)) + lower; //generate random number
arr1[i]=num; //store random number in array 1
printf("%d ",num); //print random number in array 1
}
printf(" Display Elements of Array 2 : ");
for (i = 0; i < 5; i++) {
num = (rand() % (upper - lower + 1)) + lower;
arr2[i]=num; //store random number in array 2
printf("%d ",num);//print random number in array 2
}
printf(" Largest value in Array 1 is : %d", max(arr1));
printf(" Largest value in Array 2 is : %d", max(arr2));
printf(" Smallest value in Array 1 is : %d", min(arr1));
printf(" Smallest value in Array 2 is : %d", min(arr2));
printf(" Total number of Even elements in Array 1 : %d", totalEvens(arr1));
printf(" Total number of Even elements in Array 2 : %d", totalEvens(arr2));
printf(" Total number of Odd elements in Array 1 : %d", totalOdds(arr1));
printf(" Total number of Odd elements in Array 2 : %d", totalOdds(arr2));
return 0;
}
//function to find largest value in array
int max(int arr[]){
int i;
int max=arr[0];
for(i=1; i<5; i++){
if(max<arr[i]){
max=arr[i];
}
}
return max;
}
//function to find smallest value in array
int min(int arr[]){
int i;
int min=arr[0];
for(i=1; i<5; i++){
if(min>arr[i]){
min=arr[i];
}
}
return min;
}
//function to find total number of even elements in array
int totalEvens(int arr[]){
int i;
int count=0;
for(i=0; i<5; i++){
if(arr[i]%2==0){
count+=1;
}
}
return count;
}
//function to find total number of odd elements in array
int totalOdds(int arr[]){
int i;
int count=0;
for(i=0; i<5; i++){
if(arr[i]%2!=0){
count+=1;
}
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.