GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Sep-2005, 01:18
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

C programming not giving correct values


hi, i am new to these forums and C. i am working on a project that is supposed to take data and use it in an equation. the thing is, that the teacher is going to use a different file with a diffrent # of rows. my program seems to run, but gives the incorrect values. i think i have a problem with my fscanf function. the input file has the data as shown below...

100000 2.6 1
100000 2.6 25
500000 2.6 40
100000 3.14 40
100000 3.14 41
100000 3.14 42
100000 4.01 42

heres my program...
CPP / C++ / C Code:
#include <stdio.h>
main()
{
      int CurrentYr;
      float Rate;
      int nYrs;
      int NextYr;
      int FinalYr;
      int i;
       
      FILE *fin, *fout;
      fin = fopen("cp1.dat","r");
       
      while(fscanf(fin,"%d%f%d",&CurrentYr,&Rate,&nYrs)!= EOF){
            for(i=1;i<=nYrs;i++){
                  NextYr = Rate*CurrentYr*(1-CurrentYr/1000000);
                  CurrentYr=NextYr;
            }
            FinalYr=CurrentYr;
            printf("%d\n",FinalYr);                     
            
      }
      getchar();
}


my program should be calculating one value for each row. the first column being CurrentYr, 2nd being Rate, and third being nYrs. lastly the correct values are...

234000
615385
615385
538007
780464
538007
error (reason not stated in this post)

i would really appreciate any help. i am trying to read data one line at a time, but feel i am misunderstanding how fscanf works. thank you all.

greg
  #2  
Old 12-Sep-2005, 01:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Try printing the values you are reading immediately after the fscanf() to make sure you're reading the values corectly.

CPP / C++ / C Code:
NextYr = Rate*CurrentYr*(1-CurrentYr/1000000);
I'd recommend adding a couple more parentheses just to be sure the calculation is correct -- and some spaces to make it more readable.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 12-Sep-2005, 02:01
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about
Quote:
Originally Posted by WaltP
Try printing the values you are reading immediately after the fscanf() to make sure you're reading the values corectly.

When I run this code, while trying to debug your code I encounter some errors (logical) :

CPP / C++ / C Code:
#include <stdio.h>
main()
{
      int CurrentYr;
      float Rate;
      int nYrs;
      int NextYr;
      int FinalYr;
      int i;
       
      FILE *fin, *fout;
      fin = fopen("cp1.dat","r");
       
      while(fscanf(fin,"%d%f%d",&CurrentYr,&Rate,&nYrs)!= EOF){
            for(i=1;i<=nYrs;i++){
				  printf("\n%d\t%f\t%d",CurrentYr,Rate,nYrs);
                  NextYr = Rate*CurrentYr*(1-CurrentYr/1000000);
                  CurrentYr=NextYr;
            }
            FinalYr=CurrentYr;
            printf("%d\n",FinalYr);                     
            
      }
      getchar();
}

run my example and see for yourself what you are reading into your loop.
I'd suggest reading line by line using fgets into a variable and then using sscanf to read the information stored inside the current line.

Best regards,
Kobi Hikri.

P.S

I know some people here would argue about the usage of fgets but I suggest you get to know this function.
  #4  
Old 12-Sep-2005, 02:52
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road
i made some changes...

CPP / C++ / C Code:

 #include <stdio.h>
main()
{
      float CurrentYr;
      float Rate;
      float nYrs;
      float NextYr;
      float FinalYr;
      float i;
       
      FILE *fin;
      fin = fopen( "cp1.dat", "r" );
       
      while(fscanf( fin,"%f%f%f", &CurrentYr, &Rate, &nYrs )!= EOF ){
                    i=1;
            while(i<=nYrs){
                  NextYr = Rate * CurrentYr * (1-( CurrentYr/1000000 ));
                  CurrentYr = NextYr;
                  i=i+1;
                 FinalYr = CurrentYr; }
            
                  
                  printf( "%f\n",FinalYr );                     
                  
      }
      getchar();
}


this almost gives me the correct answers, but i need them the variables CurrentYr,nYrs,i,NextYr, and FinalYr to be integers so that the answers will be those stated in my first post. the problem however is, when i change the code to initialize the variables as integers and make the proper changes to my fscanf function and printf function, i get the same wrong values i got in the very beginning. below is an example of this code...

CPP / C++ / C Code:
 #include <stdio.h>
main()
{
      int CurrentYr;
      float Rate;
      int nYrs;
      int NextYr;
      int FinalYr;
      int i;
       
      FILE *fin;
      fin = fopen( "cp1.dat", "r" );
       
      while(fscanf( fin,"%d%f%d", &CurrentYr, &Rate, &nYrs )!= EOF ){
                    i=1;
            while(i<=nYrs){
                  NextYr = Rate * CurrentYr * (1-( CurrentYr/1000000 ));
                  CurrentYr = NextYr;
                  i=i+1;
                 FinalYr = CurrentYr; }
            
                  
                  printf( "%d\n",FinalYr );                     
                  
      }
      getchar();
}


Anyone know why this is the case and how to fix it? thanks again! Greg
  #5  
Old 12-Sep-2005, 08:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Details please. "i get the same wrong values" could mean you tried to read 25 and got 51, 23.4 read as 78.00. Did you try my suggestion?
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 12-Sep-2005, 08:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fadedg2
i made some changes...

[
Anyone know why this is the case and how to fix it? thanks again! Greg

When I have a program that is giving me the "wrong" answer, I make the program tell me what it is doing. That is, in general, faster than asking some expert for help. One of the most important aspects of programming is debugging. (I say that because, in my case, I spend less than 10% of my time writing code and more than 90% of my time debugging.) It's OK to ask for help (that's what some of us like to do: help). It's more edifying to learn when it is that you can help yourself.

The hardest ones to debug are the ones that compile without any error or warning messages but don't give any output (I run the program and "nothing" happens). Or, maybe the ones that start up OK and then give some strange operating system message box with something like, "Your program crashed, would you like to send an e-mail to Bill Gates reporting this malevolent activity." The easiest ones to debug are programs that give answers but not the answers that I expected.

There are a couple of ways that I can think of that the program can give "wrong" answers:

1. The expressions aren't being evaluated. (Due, perhaps, to some error in program flow control).

2. The expressions aren't working on the values of variables that I thought they would see. (Due, perhaps, to improper input operations.)

3. Something is wrong with the expression: Either there is an error in the expression (multiplied when I should have added), or there is some difference between the way I evaluate the expression and the way that code generated by the compiler evaluates the expression (precedence rules --- need parentheses, arithmetic overflow, integer arithmetic, or some such thing).

4. All of the above (or, maybe, some combination of two or three things).

Here's my approach:

Look at the values used in the expressions. If the expressions are working on the right values but they give the "wrong" answer, then there must be something about the expression that doesn't work as I expected.
Something like this:

CPP / C++ / C Code:
  while(fscanf(fin,"%d%f%d",&CurrentYr,&Rate,&nYrs)!= EOF){
    printf("CurrentYr = %d, Rate = %f, nYrs = %d\n", CurrentYr, Rate, nYrs);
    for(i=1;i<=nYrs;i++){
        NextYr = Rate*CurrentYr*(1-CurrentYr/1000000);
        printf("CurrentYr = %d\n", CurrentYr);
        printf("CurrentYr / 1000000 = %d\n", CurrentYr / 1000000);
        printf("NextYr = %d\n", NextYr);
        CurrentYr=NextYr;
    }

Try this on an input file that has the single line
Code:
100000 2.6 2

Look at what it tells you. If you still don't understand, then post the exact output that you got. Tell us what you expected to get. What is the answer you get when you manually apply this set of input values to your formula with a calculator, or ---gasp--- pencil and paper?

Regards,

Dave
Last edited by davekw7x : 12-Sep-2005 at 09:36.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Graphics programming with TIFF file format confused_pig C Programming Language 1 10-Jan-2005 08:53
which language ? onauc C++ Forum 2 19-Nov-2004 03:53
GUI programming crystalattice C++ Forum 5 14-Sep-2004 13:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.