isn't this supposed to work.....

This is a discussion on isn't this supposed to work..... within the C Programming forums, part of the General Programming Boards category; I am writing a code to input the dimensions (less than or equal 11*11) of a 2-D array and find ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    isn't this supposed to work.....

    I am writing a code to input the dimensions (less than or equal 11*11) of a 2-D array and find the largest element of the array. I am then typcasting the address of the array into a *int and passing it to a function largest(), along with the dimensions. In largest() I am accessing the array elements using the type casted address. I have tried to verify the code for dimensions of 2*2, and 3*3, but the code doesn't seem to be working...

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int largest(int *a,int,int);
    
    int main()
    {
    	int c[12][12];
    	int a,b,d,e,l;
    
    	printf("Enter the dimensions of matrix m * n:\n");
    	scanf("%d %d",&a,&b);
    
    	printf("\nEnter elements of array (Positive):\n");
    
    	d=a; e=b;
    	a--,b--;
    	
    	
    	for(;a>=0;a--)
    		for(b=e-1;b>=0;b--)
    			scanf("%d",&c[a][b]);
    
    	l=largest((int *)c,d,e);
    
    	printf("\nLargest element = %d",l);
    }
    
    int largest(int *a,int i,int j)
    {
    	int l=0;
    	int m=j;
    
    	m=i*j;
    		
    	for(m--;m>=0;m--)
    		{
    			if(*(a+m)>l)
    				l=*(a+m);
    		}
    	return l;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    line 20 ... your for statement is broken. You should never place the intializer outside the for() statement as this can lead to very hard to trace run time errors.

    line 16 and 17 ... you are creating unnecessary variables to do easy tasks that can be done right in the for() statements

    line 24... there is no reason to typecast c (your array) as it's name decays to a pointer to it's first element.


    You really do need to get into the habit of using meaningful variable names. For example your c array should probably be called intMatrix or some such. Laziness is it's own best punishment, my friend.


    What are you going to do if your operator does any of these?

    Enter the dimensions of the matrix : -1 234567

    Enter the dimensions of the matrix : 100 50

    Enter the dimensions of the matrix : x by y

    Enter the dimensions of the matrix : -2.123 -41

    (And yes, people do enter stupid stuff and sometimes it's done just to screw things up)

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what does it do?

    at first glance, for(m--;m>=0;m--) probably isn't what you want to do. the -- is a postdecrement and it is confusing. better to initial m to what you want before the for loop spec, such as m = (i*j)-1;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dmh2000 View Post
    what does it do?

    at first glance, for(m--;m>=0;m--) probably isn't what you want to do.
    Yep... he should have used m-1 for his initializer.
    Moreover there was no need to create a new variable for that... he could simply have used j

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well the problem is, if you enter say 2x2 of data, then each of the first 2 rows is padded with 9 integers of junk, and the last 9 rows are all junk.

    Which is the problem when you try to linearly scan your matrix.

    FWIW, your use of single letter identifiers all over the place, and counting backwards really sucks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by CommonTater View Post
    there is no reason to typecast c (your array) as it's name decays to a pointer to it's first element.
    without typecasting, it flags an error: "cannot convert parameter 1 from 'int [12][12]' to 'int *'".
    All else aside, wats wrong with the logic..... can't we access the elements of a 2-D array using a pointer......

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by Salem View Post
    Well the problem is, if you enter say 2x2 of data, then each of the first 2 rows is padded with 9 integers of junk, and the last 9 rows are all junk.

    Which is the problem when you try to linearly scan your matrix.
    when we enter 2*2 data, the reverse counting for loops would fill up the first four places of the array, and then in the function largest() aren't only the first four elements supposed to be accessed and manipulated....

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    without typecasting, it flags an error: "cannot convert parameter 1 from 'int [12][12]' to 'int *'".
    All else aside, wats wrong with the logic..... can't we access the elements of a 2-D array using a pointer......
    Listen to what people are telling you...

    Don't externally initialize for() statements... it's broken and it will come back to bite you.
    Don't make intermediate variables for single uses... put it directly into the code.
    Don't use single letter variable names... they're meaningless and confusing.
    Don't use rangeless input routines... trap entries to safe values.
    Don't use uninitialized values (in arrays) and count on them being 0 ... they won't be.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by CommonTater View Post
    Listen to what people are telling you...

    Don't externally initialize for() statements... it's broken and it will come back to bite you.
    Don't make intermediate variables for single uses... put it directly into the code.
    Don't use single letter variable names... they're meaningless and confusing.
    Don't use rangeless input routines... trap entries to safe values.
    Don't use uninitialized values (in arrays) and count on them being 0 ... they won't be.
    I'll take heed of the advice and try to better my code next time. But though the spare elements of the array are being filled with junk, we're not really accessing them, so there should not be a problem....

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    I'll take heed of the advice and try to better my code next time. But though the spare elements of the array are being filled with junk, we're not really accessing them, so there should not be a problem....
    Why don't you try to better your code THIS time?

    If you clean up your mess you might find it's a lot easier to debug.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    If you know how the array is laid out in memory you can correctly access the elements on the array.

    arr[idx1][idx2]

    is equivalent to this on function largest:

    *(arr + idx1 + (idx2 * 12))

    12 varies on the length of the array. This is just a sample if you have better solution that will be great.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > aren't only the first four elements supposed to be accessed and manipulated....
    Only in the sense that you're accessing c[0][0], c[0][1], c[0][2] and c[0][3], where the last two values are garbage.

    The other two values at c[1][0] and c[1][1] are nowhere to be seen.

    You need the SAME loop you used to write the data, otherwise you're just screwing around with garbage data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am i supposed to do?
    By netmanx in forum Game Programming
    Replies: 4
    Last Post: 03-21-2008, 04:39 PM
  2. Is it supposed to do this?
    By Dribbler in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2007, 05:53 AM
  3. Replies: 1
    Last Post: 04-02-2003, 06:50 PM
  4. What am I supposed to do here? Please help!
    By marCplusplus in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 10:30 AM
  5. Is !feof supposed to work?
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 12:44 PM

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