#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Need quick help - cant find error


    HI!

    Can somebody find what is wrong with my code. He must multiply negative element of two first rows.
    Code:
    #include <stdio.h>
     
    int main() {
    
            int A[6][9];
            int i=0;
            int j;
            int number;
           
            /* inserting rows and columns */
           
            while ( i < 6 )
            {
                    j=0;
                    while ( j < 9)
                    {
                            printf("insert array[%d][%d]: ", i, j);
                            scanf("%d", &A[i][j]);
                            j++;
                    }
                    i++;
            }       
            /*Calculation*/
            arv = 0;
            while ( i < 2 )
            {
                    j=0;
                    while ( j < 9 )
                    {
                            if(A[i][j]<0)
                            {
                            			if (arv==0)
    							{
                            	        	         number += A[i][j];
    							}
    						else
                                   number *= A[i][j];       
                            }
                            j++;
                    }
                    i++;
            }
            printf("two first row negative element multiplyng result is %d\n", number);
            printf("For exit press enter");
            getchar();
            return 0;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    Have you considered using for loops instead of while loops?

    The notation is much more compact, and you might realise that you're not re-initialising a loop variable back to 0 before you start another loop.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by salem
    Have you considered using for loops instead of while loops?

    The notation is much more compact, and you might realise that you're not re-initialising a loop variable back to 0 before you start another loop.
    Our professor wants it so that we use the while loop, dont ask me why :confused:
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    So did you figure it out?

    > Our professor wants it so that we use the while loop
    Well you could write it as a for loop first, just so you know it works, then manually turn it into a while loop.
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    no i coult not figure it out :(

    By the way sorry for the estonian words in code now its fully in english.
    Code:
    #include <stdio.h>
     
    int main() {
    
            int A[6][9];
            int i=0;
            int j;
            int number;
           
            /* inserting rows and columns */
           
            while ( i < 6 )
            {
                    j=0;
                    while ( j < 9)
                    {
                            printf("insert array[%d][%d]: ", i, j);
                            scanf("%d", &A[i][j]);
                            j++;
                    }
                    i++;
            }       
            /*Calculation*/
            number = 0;
            while ( i < 2 )
            {
                    j=0;
                    while ( j < 9 )
                    {
                            if(A[i][j]<0)
                            {
                            			if (number==0)
    									{
                            	        	number += A[i][j];
    									}
    						else
                                   number *= A[i][j];       
                            }
                            j++;
                    }
                    i++;
            }
            printf("two first row negative element multiplyng %d\n", number);
            printf("For exit press enter");
            getchar();
            return 0;
    }
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    So what's the value of i at the start of the 2nd while loop?

    Have you used a debugger to step through the code?

    Did you not notice that the 2nd while loop never got executed, because i was still 6 from the exit of the first while loop?
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    It can not be true, it works now ,many many thanks.

    Im learning computer systems and i am on the first course, and our programming tacher is not very good so i dont now yet how the debugger works but i will read then about this. And as you see my english is also not very good so please try to explayne easyer.
    About the problem - I thought that the first i declarering is enough, that it is main but not locale but it wasnt so.

    I have one more task to do today, because its the last day make it. i very hope that you will help me there also.

IMN logo majestic logo threadwatch logo seochat tools logo