How to store the last sum of a row in C - c

I'm doing this exercise
"take as input a matrix of n rows and m columns of natural numbers (n and m are also
acquired from input). The program check if the sum of the numbers
contained in each row is the same for all rows"
I can't figure out how to save the last sum of the rows in the matrix. If I insert a temporary variable to allocate the last sum then it is overwritten in the next for cycle
int main ()
{
int i, j;
int sum;
int temp;
int n=3, m=3;
int matrix[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter: ");
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=matrix[i][j];
}
}
}

Keeping the code really similar to what you have right now you could do something like this:
int main (){
int i, j;
int sum = 0;
int oldsum = 0;
int temp;
int n=3, m=3;
int matrix[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter: ");
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=matrix[i][j];
}
if (i == 0 || sum == oldsum){
oldsum = sum;
}
else {
printf("The sums are not equal!\n");
return 1;
}
}
printf("All the sums are equal!\n");
return 0;
}
Since you need to check if the sum for all rows is the same you can safely stop the program when you find a sum that is different from the previous one, in this case when you find a different sum you return 1 otherwise you return 0 at the end of main.
The || in the condition is used to handle the first sum calculation when both sum and oldsum are still 0;

Add one more int array Row_sum[n]
int main ()
{
int i, j;
int sum;
int temp;
int n=3, m=3;
int matrix[n][m];
int Row_sum[n]
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter: ");
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=matrix[i][j];
}
Row_sum[i] = sum;
}
for(i = 0; i < n; i++)
{
for(j = i + 1; j < n; j++)
{
if(Row_sum[i] == Row_sum[j])
{
printf("Sum of rows in matrix are equal", i, j);
}
}
}
printf("\nSum of rows in matrix are not equal", flag);
}

Related

The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k

I want to write a program where a user tells me an integer(n) and i calculate The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k.
Ive made a function which does that But the sum is never correct. For example for n=2 it should be sum=0 but shows sum=-1 for n=3 should be sum=+2 but i shows sum=3. (Ignore the debugging printfs)
#include <stdio.h>
int athroismaAkolouthias(int n); // i sinartisi me tin opoia ypologizete to athroisma akolouthias 1+(1-2)+(1-2+3)+(1-2+3-4).....
int main(){
int n;
printf("give n: ");
scanf("%d", &n);
printf("the sum is %d", athroismaAkolouthias(n));
}
int athroismaAkolouthias(int n){
int sum1=0, sum2=0,sum=0;
int i, temp, j;
for (i=1; i<=n; i++){
for (j=1; j<=i; j++){
temp=j;
}
if (i%2==0){sum=sum-temp; printf("test1 %d%d",sum,temp);}
else{sum=temp; printf("test2 %d%d",sum,temp);}
}
return sum;
}
Your issue is with our loop which iterate with j, it should update the inner_sum based on j even/odd condition as follows:
#include <stdio.h>
int akl(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int inner_sum = 0;
for (int j = 1; j <= i; j++) {
if (j % 2 == 0) {
inner_sum -= j;
} else {
inner_sum += j;
}
}
sum += inner_sum;
}
return sum;
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", akl(n));
}
You only need two variables for sum that I named them inner_sum and sum which shows the sum of each term and sum over all terms.
Suspicious Line: else {sum = temp; ...
Shouldn't you be adding or subtracting to sum every time??
Why are you assigning to it here, without an addition or subtraction?
You also have variables sum, sum1, and sum2.
You print sum1 and sum2, but never modify them.
Here's my solution:
// The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k.
#include <stdio.h>
int ancho(int n)
{
int sum=0;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=i; ++j)
{
sum += (2*(j%2)-1)*j;
}
}
return sum;
}
int main(void)
{
int n = 5;
printf("Solution is %d\n", ancho(n));
}
// Solution is 3 for n = 5,
// because: 1 + (1-2) + (1-2+3) + (1-2+3-4) + (1-2+3-4+5) =
// 1-1+2-2+3 = 3
Output
Success #stdin #stdout 0s 5476KB
Solution is 3
IDEOne Link

Why I can't get only the first occurrence of the element in the matrix?

I'm new to the C language, and I'm trying to make a program to perform some tasks using a matrix, so I'm working on a task to search an element of the matrix and give the position (row, column) of the element's first occurrence. Below are the functions:
void Do_Search(Matrix M, int row, int col, int elem_search)
{
int i, j, aux = 0;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
if(M[i][j] == elem_search)
{
printf("\nElement first found in [%d][%d]", i, j);
break;
}
}
}
}
void Search_Element(Matrix M, int row, int col){
int i, j, num_elem, elem_search, elements = row * col;
while(num_elem > elements || num_elem < 1)
{
printf("\nEnter the number of elements to search >> ", element );
scanf("%d", &num_elem);
}
for(i = 0; i < num_elem; i++)
{
printf("\n\nEnter the element to search >> ");
scanf("%d", &elem_search);
Do_Search(M, row, col, elem_search);
}
}
In
if(M[i][j] == elem_search)
{
printf("\nElement first found in [%d][%d]", i, j);
break;
}
I would like just to get the first occurrence, but the program gives one position when there are only one occurrence and two-position despite the number of elements being higher than 2.
The break; statement only breaks out of the inner for-loop. So, the outer (row) for-loop continues to run, and thus if the element exists in multiple rows, your function will find all of them.
You probably want to return; once you find the first element, in order to exit the function after finding a single occurrence.
if(M[i][j] == elem_search)
{
printf("\nElement first found in [%d][%d]", i, j);
return;
}
You have nested loops. You are breaking out of only the inner loop.
So, you need a "stop" variable (e.g. found) to tell the outer loop to stop on a match:
void
Do_Search(Matrix M, int row, int col, int elem_search)
{
int i, j, aux = 0;
int found = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
found = (M[i][j] == elem_search);
if (found) {
printf("\nElement first found in [%d][%d]", i, j);
break;
}
}
if (found)
break;
}
}
I use a booleen variable & I initialized it to false ,if the element exist in the matrix the booleen variable initialized at true and he make Exit from the loop (while(i<row) & while(j<col))
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void Do_Search(int row, int col,int M[row][col], int elem_search);
int main(void)
{
int rows, cols;
do
{
printf("Give me the number of rows :");
scanf("%d",&rows);
}while(rows<1);
do
{
printf("Give me the number of cols :");
scanf("%d",&cols);
}while(cols<1);
int arr[rows][cols];
printf("\nThe filling of the Matrix :\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("\nDisplay of the Matrix :\n");
for (int i = 0; i < rows; i++)
{printf("\n");
for (int j = 0; j < cols; j++)
{
printf("[%d]",arr[i][j]);
}
}
printf("\n");
Do_Search(rows,cols,arr,5);
printf("\n");
}
void Do_Search(int row, int col,int M[row][col], int elem_search)
{
int i=0, j=0, aux = 0;
bool found = false;
while(i<row&&found==false)
{j=0;
while(j<col&&found==false)
{
if(M[i][j] == elem_search)
{
printf("\nElement first found in [%d][%d]", i, j);
found=true;
}
j++;
}
i++;
}
}

Used function to find the sum of the array elements, I am getting garbage value

I wrote a code for finding the sum of array elements using functions. I have written like this(mentioned below). I am getting a garbage value as output.
#include<stdio.h>
int fact(int n, int i) {
int sum = 0, arr[100];
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = fact(n, i);
printf("%d", sum);
return 0;
}
Pass the array arr to your function instead of re-declaring it inside fact
PS: Change the name of your function to something meaningful like arrSum.
Code:
#include<stdio.h>
int arrSum (int arr[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
if(n < 0 || n > 100)
return -1;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = arrSum(arr, n);
printf("%d ", sum);
return 0;
}

Kolmogrov smirinov test

Here is my. I think my code is correct but it gets stuck after i give input. But if i remove other code except sorting and printing it in ascending order it works. But if not it doesn't work.
It stuck here
#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float larges(float data[], int n);
int main()
{
printf("Kolmogorov Test\n");
int n;
float dvalue1;
//printf("No. of elements should not be greater than 20.\n");
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
//sorting in ascending order
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("\nNumbers in ascending order is: \t");
for(int i=0; i<n; i++)
{
printf("%0.2f\t",num[i]);
}
dp = dplus(&num[n], n);
dn = dminus(&num[n], n);
if(dp>dn)
{
dvalue1 = dp;
}
else
{
dvalue1 = dn;
}
//float dalphas = 0.05;
float dvalue = 0.565;
if(dvalue1 < dvalue)
{
printf("\n Since D is less tha Dalpha so the data is unformily distributed.");
}
else
{
printf("\nSince D is greater than Dalpha so the data is not uniformily distributed.");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar = larges(&data[n], n);
return lar;
}
float dminus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar;
lar = larges(&data[n], n);
return lar;
}
float larges(float data[], int n)
{
for(int i=1; i<n; i++)
{
if(data[0]<data[i])
data[0] = data[i];
}
float lar = data[0];
// printf("%f",lar);
return lar;
}
in the while loop which you used to initiating functions d plus and d minus you used while loop which is going for a infinite loop as the count value is not changing in the loop.It is going on and on and on.
You should always check the return value from scanf. That is - scanf returns the number of items matched, so you should do:
if (1 != scanf("%d", &n))
{
// Add error handling
}
If you want to repeat until the input is matched, do:
while(1)
{
printf("Enter number of elements to compute for tets: \t");
if (1 == scanf("%d", &n)) break;
printf("\nIllegal input, try again\n");
}
The same applies when reading in the float values.
That said, I don't think this is the reason, your program is stuck. To debug it, just add more printf statements. Example:
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
printf("Got %d\n", n); // DEBUG PRINT
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
printf("Got all floats\n"); // DEBUG PRINT
//sorting in ascending order
for(int i=0; i<n; i++)
{
printf("Got i = %d\n", i); // DEBUG PRINT
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
and so on.

How to find the maximum element in the matrix using function?

#include<stdio.h>
int findMax(int **a,int r,int c);
int main()
{
int a[10][10],i,j,max,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}printf("\n");}
max=findMax((int **)a,r,c);
printf("The maximum elements in the matrix is %d\n",max);
return 0;
}
int findMax(int **a,int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<r;i++)
{ for(j=1;j<c;j++)
{ if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Here I attached my coding, I need to find the maximum element present in the matrix using function, I am doing the coding, calling function is not executed, I don't know why, Help me to figure it out.
Change
int findMax(int **a,int r,int c)
to
int findMax(int (*a)[10],int r,int c)
And also,
for(i=1;i<r;i++)
{
for(j=1;j<c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
to
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
EDIT:
I think, your code should be like this:
#include<stdio.h>
int findMax(int (*a)[10],int r,int c);
int main()
{
int a[10][10],i,j,mx,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n");
mx=findMax(a,r,c);
printf("The maximum elements in the matrix is %d\n",mx);
return 0;
}
int findMax(int (*a)[10],int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Hope, it will help. :)
You can not pass a 2d array (int a[10][10]) to a pointer to pointer.
int findMax(int **a, int r, int c)
should be
int findMax(int (*a)[10], int r, int c) /* Pointer to array of 10 ints */
Use the heap if you don't know the size of the 2d array beforehand (and note that arrays are base 0):
#include <stdio.h>
#include <stdlib.h>
int findMax(int *a, int r, int c);
int main(void)
{
int *a, r, c, i, j, max;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
a = malloc(r * c * sizeof(*a));
if (a == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i * c + j]);
}
printf("The matrix is\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%d", a[i * c + j]);
}
printf("\n");
max = findMax(a, r, c);
printf("The maximum elements in the matrix is %d\n", max);
free(a);
return 0;
}
int findMax(int *a,int r, int c)
{
int t, i, j;
t = a[0];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if(a[i * c + j] > t)
t = a[i * c + j];
}
}
return t;
}
Or you can use a variable-length-array if you are under C99:
#include <stdio.h>
int findMax(int r, int c, int (*a)[]);
int main(void)
{
int i, j, max, r, c;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
int a[r][c];
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}
printf("The matrix is\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
printf("%d", a[i][j]);
}
printf("\n");
max = findMax(r, c, a);
printf("The maximum elements in the matrix is %d\n", max);
return 0;
}
int findMax(int r, int c, int (*a)[c])
{
int t, i, j;
t = a[0][0];
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
if(a[i][j] > t)
t = a[i][j];
}
}
return t;
}
This code snippet works for you.
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
a[i]=malloc(sizeof(int)*c);
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
}
Then your function call max=findMax(a,r,c); will work.
#include<stdio.h>
#include<stdlib.h>
int findMax(int **a,int m,int n)
{
int i,j,larg;
larg=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(larg<a[i][j])
larg=a[i][j];
}
}
return larg;
}
int main()
{
int m,n,i,j,larg;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
int **a=(int**)malloc(m*sizeof(int *));
for(i=0;i<m;i++)
a[i]=(int *)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d\n",&a[i][j]);
larg=findMax(a,m,n);
printf("The matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",larg);
return 0;
}

Resources