I need to find and print out the max and min number in each row of a 2D array. This is what I have tried so far.
int max(int N, int masyvas[][])
{
int i, j, max[N][N]
for(i=0; i<N; i++)
for(j=i; j<N; j++)
if(masyvas[i][j] > max) max=masyvas[i][j];
return max;
}
I know this doesn't work, but is it any close to being at least useful? Any suggestions on how should I approach this?
You're very close. The two main problems are max type (it shouldn't be
another array, but just a normal int), and the fact that you didn't reset
your max value before each row.
int i, j, max;
for(i=0; i<N; i++) {
max = 0; // reset max before checking a row
for(j=i; j<N; j++) {
if (masyvas[i][j] > max) max = masyvas[i][j];
}
}
It shouldn't be difficult to add a min check too:
int i, j, max, min;
for(i=0; i<N; i++) {
max = min = 0; // reset max & min before checking a row
for(j=i; j<N; j++) {
if (masyvas[i][j] > max) max = masyvas[i][j];
if (masyvas[i][j] < min) min = masyvas[i][j];
}
// print the values here
}
Now, you say you want to print those values. You can do so at the end of the
outer loop, as marked above. Returning the final value like in your original
code doesn't make much sense since only the final row max and min would still
be there. Another approach, which might be what you intended though, is to
make max and min an array and return a pointer:
int *maxValues = calloc(sizeof(int) * N);
int i, j;
for(i=0; i<N; i++)
for(j=i; j<N; j++)
if (masyvas[i][j] > maxValues[i]) maxValues[i] = masyvas[i][j];
return maxValues;
You probably already noticed that to return both max and min values (arrays)
you will need another level of pointers...
int **
find_max_and_min(int **masyvas)
{
int **values = malloc(sizeof(int *) * 2);
values[0] = calloc(sizeof(int) * N); // to hold min
values[1] = calloc(sizeof(int) * N); // to hold max
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
if (masyvas[i][j] > values[1][i]) values[1][i] = masyvas[i][j];
if (masyvas[i][j] < values[0][i]) values[0][i] = masyvas[i][j];
}
}
return values;
}
int **result = find_max_and_min(data);
// now result[0] is a 1d array of N size with the minimal values of each
// row i from data, and result[1] is a similar array but holding the max
// of each row.
my solution:
#include <stdio.h>
struct Output {
int value;
int pos;
};
void max(int N, int masyvas[N][N], struct Output max_by_rows[N]){
int i, j;
for(i=0; i<N; i++){
max_by_rows[i].value = masyvas[i][0];
max_by_rows[i].pos = 0;
for(j=i; j<N; j++)
if(masyvas[i][j] > max_by_rows[i].value)
{
max_by_rows[i].value=masyvas[i][j];
max_by_rows[i].pos = j;
}
}
}
void min(int N, int masyvas[N][N], struct Output min_by_rows[N]){
int i, j;
for(i=0; i<N; i++){
min_by_rows[i].value = masyvas[i][0];
min_by_rows[i].pos = 0;
for(j=i; j<N; j++)
if(masyvas[i][j] < min_by_rows[i].value)
{
min_by_rows[i].value=masyvas[i][j];
min_by_rows[i].pos = j;
}
}
}
int main(){
int N = 4;
int a[4][4] = {
{0, 1, 2, 3} ,
{7, 6, 5, 4} ,
{8, 9, 10, 11},
{15, 14, 13,12}
};
struct Output maxs[4];
struct Output mins[4];
max(N, a, maxs);
min(N, a, mins);
int i;
for(i=0; i <N; i++)
{
printf("row %d \tmax: %d\tpos: %d\tmin: %d\tpos: %d\n", i, maxs[i].value, maxs[i].pos, mins[i].value, mins[i].pos);
}
return 0;
}
output:
row 0 max: 3 pos: 3 min: 0 pos: 0
row 1 max: 7 pos: 0 min: 4 pos: 3
row 2 max: 11 pos: 3 min: 8 pos: 0
row 3 max: 15 pos: 0 min: 12 pos: 3
int i, j, max, min;
for(i=0; i<N; i++) {
max = 0;
min = 0;
for(j=i; j<N; j++ ) {
if (masyvas[i][j] > max)
max = masyvas[i][j];
if (masyvas[i][j] < min)
min = masyvas[i][j];
}
}
Related
I wanted to find the sum of each row and then sort it. I managed to do this but to make it more complicated I decided to find from which row the min sum and the max sum was originated.
Example: The row with the min sum is the 3rd and the row with the max sum is is the 5th.
The main issue in my approach that I attempted was that the b[] array (in the sortfunction below) which I used to store the initial sum of each row is now replaced with the sorted values and I cannot work with the unsorted values which would show the rows that are needed.
#include<stdio.h>
#define ROWS 5
#define COLS 3
void printnumbers(int arr[][COLS]);
void sortfunction(int arr[][COLS]);
int main() {
int arr[ROWS][COLS] = {{1, 2, 3},
{2, 3, 4},
{3, 1, 1},
{5, 5, 6},
{35, 335, 6}};
printnumbers(arr);
sortfunction(arr);
return 0;
}
void printnumbers(int arr[][COLS]){
int i,j;
for(i = 0; i<ROWS; i++)
{
printf("\n");
for(j = 0; j<COLS; j++)
{
printf("%d\t", arr[i][j]);
}
}
}
//swap function below
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// sort function
void sortfunction(int arr[][COLS]) {
int i, j, total, k, b[ROWS];
k = 0;
printf("\n");
for (i = 0; i < ROWS; i++) {
total = 0;
for (j = 0; j < COLS; j++) {
total += arr[i][j];
}
if (j == COLS) {
b[k] = total;
k += 1;
}
}
for (k = 0; k < ROWS; k++) {
printf("the sum of the %dst row is %d\n", k + 1, b[k]);
}
int a, q, min_idx;
int n = sizeof(b) / sizeof(b[0]);
// One by one move boundary of unsorted subarray
for (q = 0; q < n - 1; q++) {
// Find the minimum element in unsorted array
min_idx = q;
for (a = q + 1; a < n; a++)
if (b[a] < b[min_idx])
min_idx = a;
// Swap the found minimum element
// with the first element
swap(&b[min_idx], &b[q]);
}
for (i = 0; i < n; i++)
printf("%d ", b[i]);
printf("\n");
//Now I want to find which row has the least sum and which has the max sum but the b[] array is already sorted.
}
This prints:
the sum of the 1st row is 6
the sum of the 2st row is 9
the sum of the 3st row is 5
the sum of the 4st row is 16
the sum of the 5st row is 376
5 6 9 16 376 //it got sorted
and it needs to print also this:
The row with the min sum is in the 3rd row
The row with the max sum us un the 5th row.
One solution would be to make b an array of a struct with the sum and row numbers. The swap() and sortfunction() functions would then be as follows (including some minor simplifications):
typedef struct
{
int sum;
int row;
} tRowSum;
//swap function below
void swap(tRowSum* xp, tRowSum* yp)
{
tRowSum temp = *xp;
*xp = *yp;
*yp = temp;
}
// sort function
void sortfunction(int arr[][COLS]) {
int i, j, total;
tRowSum b[ROWS];
printf("\n");
for (i = 0; i < ROWS; i++) {
total = 0;
for (j = 0; j < COLS; j++) {
total += arr[i][j];
}
b[i].sum = total;
b[i].row = i;
}
for (i = 0; i < ROWS; i++) {
printf("the sum of the %dst row is %d\n", i + 1, b[i].sum);
}
int a, q, min_idx;
// One by one move boundary of unsorted subarray
for (q = 0; q < ROWS - 1; q++) {
// Find the minimum element in unsorted array
min_idx = q;
for (a = q + 1; a < ROWS; a++)
if (b[a].sum < b[min_idx].sum)
min_idx = a;
// Swap the found minimum element
// with the first element
swap(&b[min_idx], &b[q]);
}
for (i = 0; i < ROWS; i++) {
printf("%d (%d)", b[i].sum, b[i].row);
printf("\n");
}
}
After sorting b with respect to the sum, the original row numbers are then available for each entry.
Write a C program which can four cities temperature for last five days and display the difference between the highest and lowest temperature for each city
Sample Input
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 29
Sample Output
8
8
11
4
I was looking for the difference between large and small element in a particular row of an 2D array using C. I'm able to find large and small element but I'm kind of lost in between searching a particular row to find the difference between large and small element. Below I'm showing my piece of code -
#include<stdio.h>
int main()
{
int array1[10][10];
int num, row, column;
printf("How many rows and columns are needed: ");
scanf("%d %d", &row, &column);
printf("\nHow many cities temperature you want to enter: ");
scanf("%d", &num);
printf("\nEnter %d cities temperature: ", num);
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
scanf("%d", &array1[i][j]);
}
printf("\n");
}
int largeA = array1[0][0];
int smallA = array1[0][0];
int diff = 0;
for(int i=0; i<row; i++)
{
int diff = 0;
for(int j=1; j<column; j++)
{
if(array1[i][j] > largeA)
{
largeA = array1[i][j];
}
if(array1[i][j] < smallA)
{
smallA = array1[i][j];
}
}
diff = largeA - smallA;
printf("%d\n", diff);
}
return 0;
}
Set the min and max each time in the for(i) loop.
Use unsigned math to avoid int overflow.
// Not needed
// int largeA = array1[0][0];
// int smallA = array1[0][0];
// int diff = 0;
for(int i=0; i<row; i++)
{
//int diff = 0;
int largeA = array1[i][0]; // add
int smallA = array1[i][0]; // add
for(int j=1; j<column; j++)
{
if(array1[i][j] > largeA)
{
largeA = array1[i][j];
}
if(array1[i][j] < smallA)
{
smallA = array1[i][j];
}
}
// diff = largeA - smallA;
unsigned diff = 0u + largeA - smallA;
// printf("%d\n", diff);
printf("%u\n", diff);
}
Use functions. It makes code more readable, do not program in main
Use the correct type for indexes (size_t)
It will be much easier for you to avoid user input wnen you test your code. It will save you a lot of time spent on entering the table values.
#include <stdio.h>
int getDiff(const size_t cols, const int *arr)
{
int max = 0, min = 0;
if(arr)
{
max = arr[0]; min = arr[0];
for(size_t col = 1; col < cols; col++)
{
if(min > arr[col]) min = arr[col];
else if(max < arr[col]) max = arr[col];
}
}
return max - min;
}
int main(void)
{
int temps[][4] = {
{20, 27, 28, 22,},
{12, 22, 12, 20,},
{22, 24, 25, 33,},
{33, 30, 30, 29,},
};
for(size_t row = 0; row < 4; row++)
{
printf("Diff row %zu = %d\n", row, getDiff(4, temps[row]));
}
}
I am creating a program to print out the maximum value of each row and the minimum value of each column of an NxN 2D array.
So, for example I have this 4x4 2D array:
1 14 11 16
9 10 3 12
13 6 15 8
13 2 7 4
Then, the output should be
16 12 15 13 //Max of each row
1 2 3 4 //Min of each column
EDIT: Here is the revised code that works.
#include <stdio.h>
#include <stdlib.h>
void getMax(int n, int a[][n])
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int n, int b[][n])
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(r,a);
printf("\n");
getMin(r,a);
printf("\n");
}
This is my OLD code that did not work. Turns out I don't have to use MAX. Replace the a[][MAX] to a[][n].
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void getMax(int a[][MAX], int n)
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int b[][MAX], int n)
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(a,r);
printf("\n");
getMin(a,r);
}
Can anybody point out what I did wrong?
Thank you!
#i want to find the max value of each row, i already created an array in the main of size[5][2], and i want to find the max value for each column#
void
maximum(int a[5][2])
{
int max = a[0][0];
int i, j;
for (i = 0; i < 2; i++) {
int max = a[0][i];
for (j = 0; j < 5; j++)
if (a[j][i] > max)
max = a[j][i];
}
printf("Maximum of 1st column= %d \n", max);
for (i = 0; i < 2; i++) {
int max = a[1][i];
for (j = 0; j < 5; j++)
if (a[j][i] > max)
max = a[j][i];
}
printf("Maximum of 2st column= %d \n", max);
max = a[0][0];
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
if (a[i][j] > max)
max = a[i][j];
}
}
printf("Maximum of all %d \n", max);
}
You're doing it the opposite way. [0][i] is the first row, not the first column. You must do it like [i][0] and [i][1], and same for the j. It's max=a[i][j].
Doing like like you say only checks the max of the first and second row, leaving the other three unchecked. Hope it helps.
You need to choose carefully the indeces and their order.
#include <stdio.h>
#include <stdlib.h>
void show_max(int rows, int cols, int mat[rows][cols])
{ // ^^^^ ^^^^ ^^^^^^^^^^^^^^^ VLA notation
// Traverse the array row-major order
for (int r = 0; r < rows; ++r)
{
int max = mat[r][0];
// ^^^^ First element of row r
for (int c = 1; c < cols; ++c)
{
if (mat[r][c] > max)
max = mat[r][c];
// ^^^^
}
printf("Max of ROW %d: %d\n", r, max);
}
int max_global = mat[0][0];
// Traverse the array in colmun-major order
for (int c = 0; c < cols; ++c) // <--
{
int max = mat[0][c];
// ^^^^ // First element of column c
for (int r = 1; r < rows; ++r) // <--
{
if (mat[r][c] > max)
max = mat[r][c];
// ^^^^
}
printf("Max of COLUMN %d: %d\n", c, max);
// This could be in the first nested loop.
if (max > max_global)
max_global = max;
}
printf("Max ELEMENT: %d\n", max_global);
}
int main(void)
{
int mat[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
show_max(5, 2, mat);
}
I have a matrix that is represented by a one dimensional array,
example:
the matrix
0 1 2 3
4 5 6 7
8 9 10 11
the array
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
now give the dimensions of this matrix and the array I want to find the transpose, i.e.
0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11
I'm working in C and here is my code
#include <stdlib.h>
#include <stdio.h>
void transpose(int *array, int m, int n){
int new_array[12];
for (int i=0; i<m*n; i++) {
new_array[i] = ??;
}
for (int i=0; i<m*n; i++) {
array[i] = new_array[i];
}
}
void print_array(int array[], int size){
for (int i=0; i<size; i++) {
printf("%d\n",array[i]);
}
}
int main(){
int array[12];
for (int i=0; i<12; i++) {
array[i]=i;
}
print_array(array,12);
transpose(array,3,4);
print_array(array,12);
return 0;
}
I've tried a dozen times and failed. Is there a simple way to do this that I have missed?
Use couple of for loops to make the code easier to follow.
void transpose(int *array, int m, int n){
int new_array[12];
for (int i = 0; i < m; ++i )
{
for (int j = 0; j < n; ++j )
{
// Index in the original matrix.
int index1 = i*n+j;
// Index in the transpose matrix.
int index2 = j*m+i;
new_array[index2] = array[index1];
}
}
for (int i=0; i<m*n; i++) {
array[i] = new_array[i];
}
}
void transpose(int *array, int m, int n){
int new_array[12];
int k = 0;
for(int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
new_array[k++] = array[j*n + i];
}
}
for (int i=0; i<m*n; i++) {
array[i] = new_array[i];
}
}
Write the transpose in terms of a double loop over range [0..n) and [0..m) and calculate the indexes corresponding to the old position and the new position:
#include <stdio.h>
static void transpose(int *array, int m, int n)
{
int new_array[m * n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
int old_idx = i * n + j;
int new_idx = j * m + i;
new_array[new_idx] = array[old_idx];
}
}
for (int i = 0; i < m * n; i++)
{
array[i] = new_array[i];
}
}
static void print_array(int array[], int size)
{
for (int i = 0; i < size; i++)
{
printf(" %d", array[i]);
}
putchar('\n');
}
int main(void)
{
int array[12];
for (int i = 0; i < 12; i++)
{
array[i] = i;
}
print_array(array, 12);
transpose(array, 3, 4);
print_array(array, 12);
return 0;
}
The functions have to be pre-declared or made static to compile with my default compilation options. The transpose() function shown will work with any shape (size) of matrix (whereas the original won't work if the product of the dimensions is more than 12). I flattened the output from the array printer, too (though I'd probably make it print matrix shaped output if it were for 'production' use). I do assume that you have C99, or C11 with VLA support.
$ ./trans
0 1 2 3 4 5 6 7 8 9 10 11
0 4 8 1 5 9 2 6 10 3 7 11
$
I have changed:
for (int i = 0 ; i < m*n ; i++) {
new_array[i] = ??;
}
to:
int ctr = 0;
for (int i = 0 ; i <= m*n ; i++) {
if (ctr > m*n)
ctr -= m*n - 1;
new_array[i] = array[ctr];
ctr += n;
}
The logic is quite simple. Each row is n integers long and so the number below a num in the 2-D matrix form would be n+num
You can rewrite your transpose function as like this:
static void transpose(int *array, int m, int n)
{
int *temp=malloc(m*n*sizeof(int)); //need to create a temporary array.
memcpy(temp,array,m*n*sizeof(int));
int i, j;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
array[j*m+i]=temp[i*n+j];
}
}
free(temp);
}
You almost had it, I think this would do it...
void transpose(int *array, int m, int n){
int new_array[12];
int count;
count = 0;
for (int i=0; i < n; i++) {
for (int j=0; j < m; j += n) {
new_array[count++] = i + j;
}
}
for (int i=0; i < m * n; i++) {
array[i] = new_array[i];
}
}
This algorithm works for any number of rows an columns:
for (std::size_t i = 0; i < col; i++) {
for (std::size_t j = 0; j < row; j++) {
transpose[c++] = array[j * col + i];
}
}