C Programming 1. Create an array of 100 random numbers in the range of 1-999, wr
ID: 3568330 • 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.
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>
int main() {
int c,i,total=0, n,arr[100];
printf("Hundred random numbers in [1,100] ");
for (c = 0; c < 100; c++) {
n = rand()%999;
if(n/3==0 || n/7==0){
n=n*-1;
arr[c]=n;
}
else
{
arr[c]=n;
}
}
for(i=0;i<100;i++){
total=total+arr[i];
}
printf("%d ",total);
return 0;
}
2)
#include <stdio.h>
#include <stdlib.h>
int main() {
int c,i,j,max=0, n,arr[10][10];
printf("Hundred random numbers in [1,100] ");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
n = rand()%999;
if(n/3==0 || n/7==0){
n=n*-1;
arr[i][j]=n;
}
else
{
arr[i][j]=n;
}
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
printf(" %d",arr[i][j]);
}
printf(" ");
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(max<arr[i][j]){
max=arr[i][j];
}
}
printf("max of %d row is %d",i,max);
max=0;
printf(" ");
}
return 0;
}
3)
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows,cols,i,j,max=0,value;
printf("Enter rows");
scanf("%d",&rows);
printf("Enter cols");
scanf("%d",&cols);
int *array = malloc(100 * sizeof(int));
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < cols; y++){
printf("Enter row element value");
scanf("%d",&value);
array[x][y] = value;
}
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(max<array[i][j]){
max=array[i][j];
}
}
printf("max of %d row is %d",i,max);
max=0;
printf(" ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.