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.
Related
How can I copy the elements from a matrix,entered by user,to an array? I tried this, but it didn't work:
#include<stdio.h>
int main(){
int m[20][20],a[400],c=0;//max dimensions;
scanf("%d %d",&M,&N);//dimensions of matrix;
for(i=0;i<M;i++{
for(j=0;j<N;j++{
scanf("%d", &m[i][j]);
for(c=0;c<M*N;c++)
a[c]=m[i][j];
}}}
Don't know why you want to store both the matrix format and the array format, but anyway here is a code that should do the trick with also the data output to show the result:
#include <stdio.h>
#include <stdlib.h>
#define R 20 //max rows
#define C 20 //max columns
int main() {
int m[R][C]; //matrix
int a[R*C]; //array
int r, c; //user matrix size
int i, j; //iterators
printf("insert row size: ");
scanf("%d", &r);
printf("insert column size: ");
scanf("%d", &c);
if(r > R || c > C) {
printf("Invalid sizes");
return -1;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("insert value for row %d column %d: ", i + 1, j + 1);
scanf("%d", &m[i][j]);
a[(c * i) + j] = m[i][j];
}
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
for(i = 0; i < r * c; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Note that I also added some checks for the user data, avoiding to generate a matrix bigger that the maximum size. Also you don't need separate loops but it can be done all together.
Also please post a code that is compilable and can be run, as explained here: https://stackoverflow.com/help/how-to-ask
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);
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;
}
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].
I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9