This program is for C. PE_06_01 (Array Basics) Write a program that defines thre
ID: 3801741 • Letter: T
Question
This program is for C.
PE_06_01 (Array Basics)
Write a program that defines three integer arrays with 10 elements each. Fill each array with
random numbers from 1 to 50 using a loop and the rand() function. For each array, find the
smallest value, largest value, the total number of odd elements in each array, and the total
number of even elements in each array. Your program should display each array to the screen
followed by the minimum value, maximum value, total odd elements, and total even elements.
Create and use the functions maximum, and minimum, totalOdds, and totalEvens. You may also
want to use the printArray function to simplify programming.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int size=10;
int main(void) {
// your code goes here
int arr1[size],arr2[size],arr3[size];
createArray(arr1);
createArray(arr2);
createArray(arr3);
display(arr1);
display(arr2);
display(arr3);
return 0;
}
void createArray(int arr[])
{
int i;
int randomnumber;
for (i=0;i<size;i++)
{
randomnumber = rand() % 50;
arr[i]=randomnumber;
}
}
void display(int arr[])
{
int i,minval,maxval,NoofEven,NoofOdd;;
printf(" [");
for (i=0;i<size;i++)
{
printf("%d", arr[i]);
if (i<size-1)
{
printf(" ");
}
}
printf("]");
minval=minimum(arr);
maxval=maximum(arr);
NoofEven=totaleven(arr);
NoofOdd=totalodds(arr);
printf(" Min: %d Max: %d Even: %d Odds: %d",minval,maxval,NoofEven,NoofOdd);
}
int minimum(int arr[])
{
int minval,c,location=1;
minval = arr[0];
for ( c = 1 ; c < size ; c++ )
{
if ( arr[c] < minval )
{
minval = arr[c];
location = c+1;
}
}
return minval;
}
int maximum(int arr[])
{
int maxval,c,location=1;
maxval = arr[0];
for ( c = 1 ; c < size ; c++ )
{
if ( arr[c] > maxval )
{
maxval = arr[c];
location = c+1;
}
}
return maxval;
}
int totaleven(int arr[])
{
int NoofEven=0,c,location=1;
for ( c = 0 ; c < size ; c++ )
{
if ( arr[c] % 2== 0 )
{
NoofEven++;
}
}
return NoofEven;
}
int totalodds(int arr[])
{
int NoofOdd=0,c,location=1;
for ( c = 0 ; c < size ; c++ )
{
if ( arr[c] % 2== 1 )
{
NoofOdd++;
}
}
return NoofOdd;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.