I was given a question to solve in C.
Write a C program which can four cities temperature for last five days and display in how many days for each city temperature is higher than previous day
Sample Input
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 22
Sample Output
2
2
3
0
I've been trying to compare the elements in a particular row of an 2D array. But I'm lost in between how can I compare the elements and count although I'm able to locate large and small element in a row. I'm showing my piece of code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int array1[100][100];
int num, row, column, maxTemp = 0, minTemp = 0, counter = 0;
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");
}
maxTemp = array1[0][0];
minTemp = array1[0][0];
int maxTempRowL, maxTempColumnL, minTempRowL, minTempColumnL;
for(int i=1; i<row-1; i )
{
for(int j=1; j<column-1; j )
{
if(maxTemp < array1[i 1][j])
{
maxTemp = array1[i][j];
maxTempRowL = i; //row location
maxTempColumnL = j; //column location
}
if(minTemp > array1[i-1][j])
{
minTemp = array1[i][j];
minTempRowL = i; //row location
minTempColumnL = j; //column location
}
}
if(maxTemp > minTemp)
{
counter ;
break;
}
/*if(maxTemp <= minTemp)
{
return NULL;
}*/
printf("%d\n", counter);
counter = 0;
}
return 0;
}
CodePudding user response:
Basically you can take a single counter variable at the start of each row and if the previous element in the array is smaller than the current element, then you can add one to the counter variable. At the end of each row, you can display the count of the counter variable.
- The loop conditions were not accurate, you were starting from 1st index instead of the 0th index. Furthermore, it should have been
i <= row-1
but you wrotei < row-1
, this way the last row was not processed.
Check the code below
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int array1[100][100];
int num, row, column, maxTemp = 0, minTemp = 0, counter = 0;
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-1; i )
{
for(int j=0; j <= column-1; j )
{
scanf("%d", &array1[i][j]);
}
printf("\n");
}
for(int i=0; i<=row-1; i )
{
int counter = 0;
for(int j=1; j <= column-1; j )
{
maxTemp = array1[i][j-1];
if ( maxTemp <= array1[i][j] )
counter ;
}
printf("%d\n", counter);
}
return 0;
}
CodePudding user response:
Since you asked specifically about the comparisons, I'm going to skip the input in this answer. I'm assuming here you have a row x col matrix, witch each row representing a single city, and each column representing a single per-day-temperature-measurement.
So, if I have matrix like yours:
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 22
in order to get output like these:
2
2
3
0
I would write something like this:
for (int i = 0; i < row; i ) { // for each city
int count = 0;
for (int j = 1; j < col; j ) { // I'm starting here with i=1, because I can't compare day 0 with day -1 (day -1 doesn't exist)
if (matrix[i][j] > matrix[i][j - 1]) // If the temperature was higher than previous day
count ; // then add one to our counter
}
printf("%d\n", count); // print our count
}
and here's the whole code that I checked this solution on, in case you needed it:
#include <stdio.h>
int main() {
int row=4, col=4;
int matrix[4][4] = {
{20, 27, 28, 22},
{12, 22, 12, 20},
{22, 24, 25, 33},
{33, 30, 30, 22},
};
for (int i = 0; i < row; i ) {
int count = 0;
for (int j = 1; j < col; j ) {
if (matrix[i][j] > matrix[i][j - 1])
count ;
}
printf("%d\n", count);
}
}
Also, if you needed (I don't know why would you tho) to save this counts for later, simply create an array, like:
int counts[MAX_ARRAY_SIZE];
and instead of printing our count immediately save it in counts:
printf("%d\n", count); -> counts[i] = count;