I have a program where I enter numbers to make a matrix. Then it sums numbers in each row and prints the sum. But I need to print even the row with the biggest sum. Can someone please help me? Thanks. http://onlinemovies.pw
Here is the code:
#include <stdio.h>
int main (void)
{
static int array [10][10];
int i, j, m, n, sum = 0;
printf ("Enter the order of the matrix\n");
scanf ("%d %d", &m, &n);
printf ("Enter the co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf ("%d", &array [i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
}
You need to declare a variable and intialize it with a minimum value, if those values are always positive you can initialize it to 0:
int max = 0;
if not, you can use INT_MIN defined in <limits.h>
#include <limits.h>
...
int max = INT_MIN;
then use it in your loop:
int max = INT_MIN, imax = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
if (sum > max) {
max = sum;
imax = i;
}
sum = 0;
}
printf ("Max row (%d) is = %d\n", imax, max);
Related
everyone!
I create matrix with random elements:
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf("%d ", rand() % 10);
}
printf("\n");
}
And this is one function to sum row elements:
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j];
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
My problem is that program wont to sum it properly, program just print 0 for result.
Also, when I add manual elements, program works fine, but wont with random numbers.
Someone to help me? Thank you!
You haven't actually assigned the values to the array; you're just printing them.
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
int num = rand() % 10;
printf("%d ", num);
array[i][j] = num;
}
printf("\n");
}
is probably what you want.
Also the array has space for only 10 x 10 elements. So you need to validate that the inputs for m and n do not exceed that limit.
The default value of int type in C is 0. Because you do not enter the value to array, so all of values of this array are equal to 0. So sum = 0 after all calculations.
Try to initialize values for this array. For example, in the first for loop (the for loop content printf with rand() function):
array[i][j] = rand() % 10;
OR
array[i][j] = 1; // or whatever you want
Hi all i have a problem with this exercise in Language C.
The exercise is:
Given a matrix write a function that:
A) Calculate and return the sum of the elements.
B) Calculate and return the average of the i-th line
I did my own procedure but i have a lot errors.
My procedure is:
#include <stdio.h>
#include <stdlib.h>
void main(){
int n=10;
int m=10;
int i;
int j;
int mat [i][j];
int sum=0;
for (i=0;i<n;i++){
for (j=0; j<m;j++)
sum=sum+mat[i][j];}
printf("The sum of all elements of matrix is:%d",sum);
somma=0;
for (j=0;j<m;i++){
sum=sum+mat[i][j];
sum=sum/m
printf("The average of i-th line is:%d",sum);
}
}
i think that i have to put scanf somewhere but i don't know where.
I hope that you can help me
thank you!
You declare a matrix with undefined sizes
int mat [i][j];
where i and j are uninitizlized.
You probably want
int mat [n][m];
Moreover your matrix should be inizialized with values, otherwise you'll get the sum of stack garbage.
At the end, a possible solution is
#include <stdio.h>
int main(void)
{
int n = 2;
int m = 2;
int i;
int j;
int mat[n][m];
int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("Insert value of mat[%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
sum = sum + mat[i][j];
}
}
printf("The sum of all elements of matrix is: %d\n", sum);
for (i = 0; i < n; i++)
{
sum = 0;
for (j = 0; j < m; j++)
{
sum = sum + mat[i][j];
}
sum = sum / m;
printf("The average of line %d is: %d\n", i, sum);
}
}
As you can see I changed the average calculation:
First of all you wrote a j loop incrementing i
You must loop for all lines, so you must add a for loop that inc rows
sum must be reset each time you calculate the row average
take notes that the average is calculated using integers, so no decimals will be available.
So i've been trying to do a code that adds the integers of a row in an array (this integers can be negative for example)
So you will have something like this:
input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5
output:
19
3
-7
What I've done is this:
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
}
I can't find a way to "add" negative numbers, what should I change/add to this code? Thanks again :)
#include <stdio.h>
int main (){
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
return 0;
}
include <stdio.h>
int main(){
int number [5][5] , row=0, col=0,sumrow[5], sumcol[5];
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5,sumrow[row]=sumcol[col]=0;row++);
for(row=0;row<5;row++)
for(col=0;col<5;col++)
{
sumrow[row]=sumrow[row]+number[row][col];
sumcol[col]=sumrow[col]+number[row][col];
}
// here I want to plus all the row in sumrow and col in sumcol
printf("Row totals ");
for(row=0;row<5;row++) {
printf("%d ",sumrow[row]);
}
printf("\n Row totals ");
for(col=0;col<5;col++){
printf("%d ",sumrow[col]);
}
return 0;
}
This code is not working. I want to read the matrix and all rows and columns.
I don't know what the problem is.
If u want to add all elements of row and column or sum of all rows and column below is the code,
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
int rowSum = 0;
int colSum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
rowSum += sum;
sum = 0;
}
printf("Sum of all the row is = %d\n", rowSum);
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
colSum += sum;
sum = 0;
}
printf("Sum of all the column is = %d\n", colSum);
}
I don't know if I understood correctly, but if you want to sum all the elements of the matrix you should use this for example:
int a[5][5],n,m,i,j, soma=0;
printf("Enter value of m and n: ");
scanf("%d%d",&m,&n);
printf("\nEnter elemets of the matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
for(i=0;i<m;++i){
sum_col += i;
}
for(j=0;j<n;++j){
sum_row += ;
}
printf("\nYour sum is: %d", soma);
getch();
return 0;
PS: I also used conio library, so you should use #include<conio.h> at the beginning
But if you want to sum the numb of rows and the numb of cols and print one by one, you should use this one:
int number [5][5] , row=0, col=0,sumrow=0, sumcol=0, cont_1=0, cont_2=0;
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5;row++){
cont_1++;
for(col=0;col<5;col++)
{
sumcol += number[row][col];
}
printf("\nThe sum of Col %d is %d", cont_1, sumcol);
sumcol = 0;
}
for(col=0;col<5;col++){
cont_2++;
for(row=0;row<5;row++)
{
sumrow += number[row][col];
}
printf("\nThe sum of Row %d is %d", cont_2, sumrow);
sumrow = 0;
}
return 0;
I just made your code more clear, so you should understand what you are doing.
The problem statement goes like this: Calculate the sum of the elements in a bidimensional matrix, using a separate thread to calculate the sum of each row. The main thread adds up these sums printing the final result.
From what I have seen so far, the code runs correctly. The only issue is when I choose the number of rows to be smaller than the number of columns (e.g. rows = 2, columns = 3), because it only computes the sum for the first 2 columns, completely ignoring the third one.
This is the code I wrote in C and I would really appreciate any help understanding what I did wrong or missed. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define M 10
#define N 10
int rows, columns, a[M][N], s[M];
// compute the sum of each row
void* f(void* p) {
int k = *((int*) p);
int i;
for (i = 0; i < columns; i++) {
s[i] += a[k][i];
}
return NULL;
}
int main() {
int i, j, *p, rc;
int sum = 0;
pthread_t th[M];
// matrix creation
printf("no. of rows = ");
scanf("%d", &rows);
printf("no. of columns = ");
scanf("%d", &columns);
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("a[%d][%d] = \n", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is: \n");
for(i=0; i < rows; i++) {
for(j=0; j < columns; j++)
printf("%d ", a[i][j]);
printf("\n");
}
// thread creation
for (i=0; i < rows; i++) {
p = malloc(sizeof(int));
*p = i;
rc = pthread_create(&th[i], NULL, f, p);
if (rc != 0) {
printf("Thread creation failed");
exit(-1);
}
}
for (i=0; i < rows; i++) {
pthread_join(th[i], NULL);
}
// compute the final sum
for (i=0; i < rows; i++) {
sum += s[i];
}
printf("The sum is = %d\n", sum);
return 0;
}
You need
s[k] += a[k][i];
instead of
s[i] += a[k][i];
Sum of each rows should be added up in each index of each row as s is declared as s[M].