C Programming 1. Create an array of 100 random numbers in the range of 1-999, wr
ID: 3690117 • Letter: C
Question
C Programming
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. •Return a count of the number of even values •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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void cien(int hundred[])
{
int i = 0;
int range = (999 - 1) + 1;
for (i = 0; i < 100; i++)
{
hundred[i] = rand() % range + 1;
if (hundred[i] % 3 == 0 || hundred[i] % 7 == 0)
{
hundred[i] = hundred[i] * -1;
printf("%d ", hundred[i]);
}
else
{
printf("%d ", hundred[i]);
}
}
printf(" ");
return;
}
int even( int hundred[])
{
int i;
int count = 0;
for ( i = 0; i < 100; ++i)
{
if (hundred[i] %2 == 0)
{
count++;
}
}
return count;
}
int total(int hundred[])
{
int i;
int sum = 0;
for ( i = 0; i < 100; ++i)
{
sum = sum + hundred[i];
}
return sum;
}
int two_dim(int hundred[], int arr[][10])
{
int x;
int y;
int i = 0;
int table[10][10];
while (i != 100)
{
for (x = 0; x <10; x++)
{
for (y = 0; y < 10; y++)
{
arr[x][y] = hundred[i];
printf("%d ", arr[x][y]);
i++;
}
}
}
return **table;
}
void hi_row(int arr[][10])
{
printf(" ");
int i,j;
for ( i = 0; i < 10; ++i)
{
int high = arr[i][0];
for ( j = 0; j < 10; ++j)
{
if(arr[i][j] > high) high = arr[i][j];
}
printf("Highest in row %d is %d ",i, high);
}
}
void** cust_arr()
{
printf(" ");
int r, c;
printf("Enter row: ");
scanf("%d",&r);
printf("Enter column: ");
scanf("%d",&c);
int **arr;
int count = 0,i,j;
arr = (int **)malloc(sizeof(int *) * r);
arr[0] = (int *)malloc(sizeof(int) * c * r);
for(i = 0; i < r; i++)
arr[i] = (*arr + c * i);
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);
printf(" ");
}
return 0;
}
int main(void)
{
int hundred[100];
int table[10][10];
cien(hundred);
int count = even(hundred);
printf("count of the number of even values: %d ",count);
int tot = total(hundred);
printf("sum of all values in the array: %d ", tot);
two_dim(hundred, table);
hi_row(table);
cust_arr();
//system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.