C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2013, 03:19 PM
sissiv6itleja sissiv6itleja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 sissiv6itleja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 41 sec
Reputation 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;
}
 




Reply With Quote
  #2  
Old January 13th, 2013, 03:21 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 11 h 48 m 41 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old January 13th, 2013, 03:24 PM
sissiv6itleja sissiv6itleja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 sissiv6itleja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 41 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old January 13th, 2013, 03:31 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 11 h 48 m 41 sec
Reputation Power: 1774
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.

Reply With Quote
  #5  
Old January 13th, 2013, 03:39 PM
sissiv6itleja sissiv6itleja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 sissiv6itleja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 41 sec
Reputation 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;
}
 

Reply With Quote
  #6  
Old January 13th, 2013, 03:42 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 11 h 48 m 41 sec
Reputation Power: 1774
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?

Reply With Quote
  #7  
Old January 13th, 2013, 04:01 PM
sissiv6itleja sissiv6itleja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 sissiv6itleja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 41 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need quick help - cant find error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap