Sunday, July 12, 2009

Help with C programming homework pls! =X?

Hey guys! I really need help on this question! I've been stuck on this problem for a week %26gt;.%26lt; 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 %26gt;.%26lt;!! It's still incomplete:





#include %26lt;stdio.h%26gt;


main()


{


int a, b;


FILE *fin;


fin=fopen("filein.dat", "r");





fscanf(fin, "%d", %26amp;a);


fscanf(fin, "%d", %26amp;b);





while( fscanf(fin, "%d", %26amp;a) !=EOF) {


while(a%26lt;b) {


fscanf(fin, "%d", %26amp;a);


fscanf(fin, "%d", %26amp;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

Help with C programming homework pls! =X?
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 %26lt;stdio.h%26gt;


main() {


FILE *fin = fopen("filein.dat", "r");


int prevValue, curValue;





/* initialize the prevValue with the


* first number


*/


fscanf(fin, "%d", %26amp;prevValue);





/* Now, get each subsequent number.


*/


while (fscanf(fin, "%d", %26amp;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 %26lt; 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");


}
Reply: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 %26lt;stdio.h%26gt;


main()


{


int a, b;


FILE *fin;


fin=fopen("filein.dat", "r");





fscanf(fin, "%d", %26amp;a);


fscanf(fin, "%d", %26amp;b);





while( fscanf(fin, "%d", %26amp;a) !=EOF) {


while(a%26lt;b) {


fscanf(fin, "%d", %26amp;a);


fscanf(fin, "%d", %26amp;b);


}


printf("NO\n");


}


No comments:

Post a Comment