English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hey guys! I really need help on this question! I've been stuck on this problem for a week >.< Here it goes:

Write a program that reads integers until end-of-file and then prints YES if the numbers do not decrease at all and NO otherwise. (eg: 1, 2, 3 gives YES | 1, 0, 3 gives NO)

I havent even made it to the compiler yet coz I can't even get the code to make sense to myself >.
#include
main()
{
int a, b;
FILE *fin;
fin=fopen("filein.dat", "r");

fscanf(fin, "%d", &a);
fscanf(fin, "%d", &b);

while( fscanf(fin, "%d", &a) !=EOF) {
while(a fscanf(fin, "%d", &a);
fscanf(fin, "%d", &b);
}
printf("NO\n");
}

which is obviously horribly horribly wrong. I'm only in my 2nd week in this programming module so I'm completely noobish =X Can someone please teach me? Thanks!

Oh, and please don't use any complicated codings coz my tutor won't believe I know all that =x

2007-08-23 04:08:20 · 2 answers · asked by axeile 2 in Computers & Internet Programming & Design

Thanks!! I've finally got it! =D

2007-08-23 05:19:22 · update #1

2 answers

You don't want to read TWO values at a time. You're not looking at pairs in the file. With the algorithm you had written, where you read a+b, then a+b again, imagine that your input file was:

1 8 2 9

Then you'd read 1[a] and 8[b], and it wouldn't decrease. Next you'd read 2[a] and 9[b] and it wouldn't decrease. But you'd miss the decrease from 8 to 2.

You need to look at each individual value and see if it is less than the last value you read. You can do this by storing the previous read value, and comparing it to the current read value (and then shifting the 'current' to 'previous' before reading again).

=======================
#include
main() {
FILE *fin = fopen("filein.dat", "r");
int prevValue, curValue;

/* initialize the prevValue with the
* first number
*/
fscanf(fin, "%d", &prevValue);

/* Now, get each subsequent number.
*/
while (fscanf(fin, "%d", &curValue) !=EOF) {

/* See if it's less than the previous.
* If so, we can print 'NO' and exit.
* No need to read the rest of the file.
*/
if (curValue < prevValue) {
printf("NO\n");
exit(0);
}

/* At the end of the loop, the 'current'
* value becomes the 'previous' as we
* prepare to head back to the top of
* the loop, to read the next one.
*/
prevValue = curValue;
}

/* If we hit EOF and haven't seen a
* decrease, print 'YES'
*/
printf("YES\n");
}

2007-08-23 05:13:09 · answer #1 · answered by McFate 7 · 0 0

I'm a c++ programmer but I think you are on the right track.
You only have problem. You said if the numbers don't decrease
at all right?

Then just take the first number store is at a maximum, if the next number is larger than store THAT as the maximum etc... if a number is ever smaller just trigger a boolean flag to be false, and print "NO" and exit the program. Otherwise if the boolean is true at the end of the loop then print "yes";



#include
main()
{
int a, b;
FILE *fin;
fin=fopen("filein.dat", "r");

fscanf(fin, "%d", &a);
fscanf(fin, "%d", &b);

while( fscanf(fin, "%d", &a) !=EOF) {
while(a fscanf(fin, "%d", &a);
fscanf(fin, "%d", &b);
}
printf("NO\n");
}

2007-08-23 04:26:50 · answer #2 · answered by icefyre 5 · 0 0

fedest.com, questions and answers