Write the following program in C: 1. Create an array of 100 random numbers in th
ID: 3693126 • Letter: W
Question
Write the following program in C:
1. Create an array of 100 random numbers in the range of 1…999, write a function for each of the following processes. In building the array, if 3 or 7 evenly divide the random number, store it as a negative number.
a. Print the array ten values to a line. Make sure that the values are aligned in rows.
b. Return a count of the number of even values
c. Return the sum of all values in the array
2. Create a two dimensional array (size 10 X 10). Fill this two dimensional array with the values from the above single dimensional array. Determine the maximum value in each row. Display the two-dimensional array and the maximum of each row.
3. Repeat number 2 above but this time instead of 10 X 10 array, prompt the user for the size of the row and column, allow user to fill in the values and display the array.(Hint: Use pointers and dynamic memory allocation )
Explanation / Answer
1)
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void print(int arr[]){
for(int i=0; i<100; i++){
if(i%10==0)
printf(" ");
printf("%d ",arr[i]);
}
}
int countEven(int arr[]){
int count = 0;
for(int i=0; i<100; i++){
if(arr[i]%2==0)
count++;
}
return count;
}
int sum(int arr[]){
int sum = 0;
for(int i=0; i<100; i++){
sum = sum + arr[i];
}
return sum;
}
int main()
{
int arr[100];
srand(time(NULL));
for(int i=0; i<100; i++){
arr[i] = ( rand() % 999 ) + 1;
}
print(arr);
printf("Even count = %d",countEven(arr));
printf("Sum = %d",sum(arr));
return 0;
}
2)
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
void print(int arr[]){
for(int i=0; i<100; i++){
if(i%10==0)
printf(" ");
printf("%d ",arr[i]);
}
}
int countEven(int arr[]){
int count = 0;
for(int i=0; i<100; i++){
if(arr[i]%2==0)
count++;
}
return count;
}
int sum(int arr[]){
int sum = 0;
for(int i=0; i<100; i++){
sum = sum + arr[i];
}
return sum;
}
void get2D(int arr[], int t[10][10]){
int c=0;
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
t[i][j] = arr[c++];
}
}
}
int main()
{
int arr[100];
srand(time(NULL));
for(int i=0; i<100; i++){
arr[i] = ( rand() % 999 ) + 1;
}
print(arr);
int t[10][10];
get2D(arr,t);
printf(" 2D Array: ");
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
printf("%d ",t[i][j]);
}
printf(" ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.