The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need quick help - cant find error
Discuss Need quick help - cant find error in the C Programming forum on Dev Shed. Need quick help - cant find error C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 13th, 2013, 03:19 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
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;
}
|

January 13th, 2013, 03:21 PM
|
 |
Contributed User
|
|
|
|
|
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.
|

January 13th, 2013, 03:24 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
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 
|

January 13th, 2013, 03:31 PM
|
 |
Contributed User
|
|
|
|
|
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.
|

January 13th, 2013, 03:39 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
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;
}
|

January 13th, 2013, 03:42 PM
|
 |
Contributed User
|
|
|
|
|
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?
|

January 13th, 2013, 04:01 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 7
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|