Help with matrix

This is a discussion on Help with matrix within the C Programming forums, part of the General Programming Boards category; hello i got problem with this, its really simple but i dunno wheres my mistake or maybe i am copying ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Help with matrix

    hello i got problem with this, its really simple but i dunno wheres my mistake or maybe i am copying the matrixs in wrong way
    i need to find the biggest element and sum up other elements in his horizontal line minus the biggest one. All this need to be copyed in new matrix with same elements but in the spot where the biggest one was i need to put the sum mentioned befor.

    Code:
    #include<stdio.h>
    int main()
    {
    int a[100][100],b[100][100],i,j,m,n,max,z=0,x=0,y=0;
    printf(".........\n");
    scanf("%d",&m);
    scanf("%d",&n);
    printf("..........\n");
    for(i=0;i<m;i++)
    	for(j=0;j<n;j++)
    		{
    			printf("a[%d][%d]=",i,j);
    			scanf("%d",&a[i][j]);
    		}
    max=a[0][0];
    for(i=0;i<m;i++)
    	for(j=0;j<n;j++)
    	if(a[i][j]>max)
    	{
    		max=a[i][j];
    		x=i;
    		y=j;
    	}
    	for(i=x;i=x;i=x)
    		for(j=0;j<n;j++)
    		z+=a[i][j];
    		z-=max;
    for(i=0;i<m;i++)
    	for(j=0;j<n;j++)
    		b[i][j]=a[i][j];
    	b[x][y]=z;
    			for(i=0;i<m;i++)
    			{	for(j=0;j<n;j++)
    				printf("%d",b[i][j]);
    				printf("\n");
    			}
    			return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My guess is that you are forgetting to wrap something up in braces. Any statement alone is considered to be alone, unless it is grouped by a pair of braces:
    Code:
    for( ... )
        this goes with the for loop;
        this does not;
    Other than that, your indentation really discourages me from reading through that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Nope, its good as it is. i only need the first statement in the loop so i can sum up the elements. I think my mistake is from 28th line ...
    I want to copy the old matrix into new one but with one changed element
    eg.
    Code:
                    1    2    3             1    2    3
                    4    5    6       ->    4    5    6                   max==8; sum=-1+8+3; sum-=max; sum ==2
                   -1    8    3            -1    2     3

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your indentation is terrible. Maybe it looks ok to you in where ever you pasted this from, but you can preview your post here you know. Or you could always hit edit.
    Code:
    #include<stdio.h>
    int main()
    {
        int a[100][100],b[100][100],i,j,m,n,max,z=0,x=0,y=0;
        printf(".........\n");
        scanf("%d",&m);
        scanf("%d",&n);
        printf("..........\n");
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",&a[i][j]);
            }
    
        max=a[0][0]; /* not attached to any loop */
    
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                if(a[i][j]>max)
                {
                    max=a[i][j];
                    x=i;
                    y=j;
                }
    
        for(i=x;i=x;i=x) /* what on earth are you doing? */
            for(j=0;j<n;j++)
                z+=a[i][j];
    
        z-=max; /* not part of any loop */
    
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                b[i][j]=a[i][j];
    
        b[x][y]=z; /* not part of any loop */
    
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
                printf("%d",b[i][j]);
            printf("\n");
        }
        return 0;
    }
    There, nice and readable.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 10:28 AM
  2. Replies: 1
    Last Post: 12-31-2010, 04:43 PM
  3. adjacency matrix and sparse matrix
    By dpp in forum C Programming
    Replies: 3
    Last Post: 07-11-2010, 11:26 AM
  4. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 03:48 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 01:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21