Create a program that processes two-dimensional array according to task 1. Defin
ID: 3568060 • Letter: C
Question
Create a program that processes two-dimensional array according to task
1. Define array dimensions as constants.
2. Fill array with random numbers in range R (-188 .. -4).
3. Processing of array should be implemented as function (using indices).
4. Displaying the content of array should be implemented as function (using pointers):
5 12 -4 8 2
8 3 3 -16 1
22 215 34 0 -11
88 13 22 6 5
81 76 23 56 118
5. Program should output text massage in case of errors.
6. Program has to be written in C.
Swap the column that includes the biggest number of negative elements with the
column that includes the biggest number of positive elements.
Explanation / Answer
#include<stdio.h>
#define ROWS 5
#define COLUMNS 5
int randomNoBetween(int x, int y) {
return (random() %(y-x+1))+x;
}
void initializeArray(int values[][COLUMNS],int x,int y)
{
int i,j;
for( i =0;i<ROWS;i++)
{
for( j = 0 ;j< COLUMNS ; j++)
{
values[i][j] = randomNoBetween(x,y);
}
}
}
int findMaximumRowId(int values[][COLUMNS])
{
int max = 0,rowid=0,i,j;
for( i =0;i<ROWS;i++)
{
for( j = 0 ;j< COLUMNS ; j++)
{
if(values[i][j] > max)
{
rowid = i;
max = values[i][j];
}
}
}
return rowid;
}
int findMinimumRowId(int values[][COLUMNS])
{
int min = values[0][0],rowid=0,i,j;
for( i =0;i<ROWS;i++)
{
for( j = 0 ;j< COLUMNS ; j++)
{
if(values[i][j] < min)
{
rowid = i;
min = values[i][j];
}
}
}
return rowid;
}
void swap(int values[][COLUMNS])
{
int minRowid = findMinimumRowId(values);
int maxRowid = findMaximumRowId(values);
int temp[COLUMNS],i,j;
for(i = 0 ; i< COLUMNS ; i++)
temp[i] = values[minRowid][i];
for(i = 0 ; i< COLUMNS ; i++)
values[minRowid][i] = values[maxRowid][i];
for(i = 0 ; i< COLUMNS ; i++)
values[maxRowid][i] = temp[i];
}
void display(int *values)
{
int i,j;
for( i =0;i<ROWS;i++)
{
for( j = 0 ;j< COLUMNS ; j++)
{
printf("%d ", *((values+i*COLUMNS) + j));
}
printf(" ");
}
}
int main()
{
int values[ROWS][COLUMNS],x,y;
printf("Enter range:");
//scanf("%d %d",x,y);
x = -188;y=188;
initializeArray(values,x,y);
display(values);
swap(values);
printf(" ");
display(values);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.