Like Tree2Likes

Please check my code

This is a discussion on Please check my code within the C Programming forums, part of the General Programming Boards category; Could someone please check my code and tell me what I'm doing wrong. The problem lies in the second function ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Please check my code

    Could someone please check my code and tell me what I'm doing wrong.
    The problem lies in the second function and possibly the second while statement. I am trying to read from the second infile, which needs to be the searchInt array, then search that array and return the locations of some integers in the file. Those integers need to be linked to some scores from the first in file then the two numbers need to be printed in the outfile. It should look something like this.
    Just the stuff from the searchInt function:

    The first column of numbers are from the infile and are the array, the second column is what needs to be found from the first Infile.
    1 7
    5 8

    I apologize if I didn't explain that very well, so if you have questions let me know. I appreciate any help I can get on this. I am new to this and I just don't quite get it. I will say this is for a class, I have asked for help but I still don't understand it. I would appreciate if someone could tell me how I need to link the variables to the array and then back to the main part of the file. Thanks ahead of time for your advice.

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #define MAX 40
    
    //int summaryResults(int [],int []);
    void printArrayLF(int [],int [],int,FILE *);
    int searchInt(int [],int ,int );
    
    main()
     {
      int id[MAX];
      int score[MAX];    
      int idNum,j1,j2,j3,totalScore,ctr,total,e,i,idnbr;
      float Avg;
      FILE *InFile,*inFile, *OutFile;
      InFile=fopen("file1.txt","r");
      inFile=fopen("file2.txt","r");
      OutFile=fopen("outproj5.txt","w");
      printf("Candidate Results\n");
      printf(" Id     J1     J2     J3     score\n");
      fprintf(OutFile,"Candidate Results\n");
      fprintf(OutFile," Id     J1     J2     J3     score\n");
      for (i=0;i<MAX;i++)
       {  
        id[i]=0;
        score[i]=0;
        }
      ctr=0;
      total=0;
      e=fscanf(InFile,"%d %d %d %d",&idNum,&j1,&j2,&j3);
      while(e==4)
       {
         totalScore = j1+j2+j3;
         id[ctr]=idNum;
         score[ctr]=totalScore;
         printf("%3d %6d %6d %6d %9d\n",idNum,j1,j2,j3,totalScore);
         fprintf(OutFile,"%3d %6d %6d %6d %9d\n",idNum,j1,j2,j3,totalScore);
         ctr=ctr+1;
         total = totalScore + total;
         e=fscanf(InFile,"%d %d %d %d",&idNum,&j1,&j2,&j3); 
        }
       Avg=1.0*total/ctr;
       printf("          %d Total Candidates\n",ctr);
       fprintf(OutFile,"          %d Total Candidates\n",ctr);
       printf("          Average Total Score is %.1f \n",Avg);
       fprintf(OutFile,"          Average Total Score is %.1f\n",Avg);
       printArrayLF(id,score,ctr,OutFile);
       printf("Score Check\n");
       fprintf(OutFile,"Score Check\n");
       e=fscanf(inFile,"%d",id);
       while (e==1)
        {
         int scoreCheck,idNum;
         id[ctr]=idNum;
         scoreCheck=searchInt(id,ctr,idnbr);
         if (scoreCheck<0)
             {
             printf("not found\n");
             fprintf(OutFile,"not found\n");
              }
            else
              {
               scoreCheck=searchInt(id,ctr,idnbr);
               printf("%3d %10d\n",idNum,score);
               fprintf(OutFile,"%3d %10d\n",idNum,score);
              }
         e=fscanf(inFile,"%d",id);
        }
       printf("End of Program\n");
       fprintf(OutFile,"End of Program\n");
       fclose(OutFile);
       fclose(InFile);
       system("pause");
     }
     
    void printArrayLF(int a[],int b[],int ctr,FILE *fl)
       {
         int k;
         printf("\n Summarized Results\n");
         fprintf(fl,"\n Summarized Results\n");
         printf(" Id     totalScore\n");
         fprintf(fl," Id     totalScore\n");
         for(k=0;k<ctr;k++)
             {
               printf("%3d %10d\n",a[k],b[k]);
               fprintf(fl,"%3d %10d\n",a[k],b[k]); 
             }
          fprintf(fl,"\n");
          printf("\n");
       }
       
    int searchInt(int a[],int ctr,int idnm)
       {
         int k;
         int loc=-1;
         for(k=0;(k<ctr)&&(loc==-1);k++)
               if(a[k] == idnm)
                    loc = k;
         return loc;
         }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (e==1)
    {
        int scoreCheck,idNum;
        id[ctr]=idNum;
        scoreCheck=searchInt(id,ctr,idnbr);
        if (scoreCheck<0)
        {
            printf("not found\n");
            fprintf(OutFile,"not found\n");
        }
        else
        {
            scoreCheck=searchInt(id,ctr,idnbr);
            printf("%3d %10d\n",idNum,score);
            fprintf(OutFile,"%3d %10d\n",idNum,score);
        }
        e=fscanf(inFile,"%d",id);
    }
    You have already done that search, so you don't need to do it again, because you will get the same result. scoreCheck already stores your answer if you are at the else block. You also don't actually use scoreCheck except for the if check. Is score supposed to be scoreCheck and id supposed to be idNum in those statements?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Cool, thanks quzah. I made the change. However, I forgot to mention that the searchInt array isn't returning the values that I want it to return and I'm not sure why. I think its returning a -1 instead of the integer values I'm trying to read in. Any word on how to fix that?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Unfortunately as far as what is supposed to be what thats what I'm not sure of, with id and score. I am unsure of what variables I need to use to get the correct information. Id is what numbers I am trying to read in to the array, the id's are used in both infiles, but the second infile has just id numbers and I assume it should read those integers then relate them to the id numbers and scores in the second infile then print both id number and the related score. But again I'm just not sure how i need to state the variables or whatever in the code. In the main part of the file the arrays are id[] and score[] but I don't know if that is what they should be for the second function and array.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Yes, you write a test case and debug it.
    Like
    Code:
    int main ( ) {
       int array[] = { 1, 2, 3, 4, 5 };
       int result;
       result = searchInt(array,5,2);
       printf("Search result (success)=%d\n", result );
       result = searchInt(array,5,99);
       printf("Search result (fail)=%d\n", result );
       return 0;
    }
    stahta01 and Phenom like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I still don't know what you are trying to do:
    The first column of numbers are from the infile and are the array, the second column is what needs to be found from the first Infile.
    1 7
    5 8
    Your file has 2 columns. The first one says what? Go to line 1 and see if it's 7?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    RE:Please check my code

    Code:
    Hi,
    
    Actually u did some wrong thing in line "e=fscanf(inFile,"%d",id);"
    I changed your code as below.( Highlighted using RED color).Please check it.
    
    
    ========================================================================================
    
     e=fscanf(inFile,"%d",&idnbr); 
       while (e==1)
        {
         int scoreCheck;
         scoreCheck=searchInt(id,ctr,idnbr);
         if (scoreCheck<0)
             {
             printf("not found\n");
             fprintf(OutFile,"not found\n");
              }
            else
              {
               scoreCheck=searchInt(id,ctr,idnbr);
               printf("%3d %10d\n",id[scoreCheck],score[scoreCheck]);
               fprintf(OutFile,"%3d %10d\n",id[scoreCheck],score[scoreCheck]);
              }
         e=fscanf(inFile,"%d",&idnbr);
        }
       printf("End of Program\n");
       fprintf(OutFile,"End of Program\n");
       fclose(OutFile);
       fclose(InFile);
       fclose(inFile);
       system("pause");
     }
    
    =======================================================================================================

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    since u r new so take an advice.
    Next time you post a code please try to do commenting in your code....
    That would help us to help you in better ways.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try your search function like this....
    Code:
    int searchInt(int a[],int ctr,int idnm)
       {
         int k;
         for (k = 0; k < ctr; k++)
            if (a[k] == idnm)
              return k;
         return -1;
         }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Try your search function like this....
    Code:
    int searchInt(int a[],int ctr,int idnm)
       {
         int k;
         for (k = 0; k < ctr; k++)
            if (a[k] == idnm)
              return k;
         return -1;
         }
    Note that this does not necessarily give the same output as the first function.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Note that this does not necessarily give the same output as the first function.
    It returns... K if idnm is matched or -1 if it's not matched after the loop completes.
    I don't see the problem...

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    It returns... K if idnm is matched or -1 if it's not matched after the loop completes.
    I don't see the problem...
    Code:
    int array[6] = { 1,2,3,4,5,1 };
    int answer = searchInt( array, 6, 1 );

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Code:
    int array[6] = { 1,2,3,4,5,1 };
    int answer = searchInt( array, 6, 1 );
    Quzah.
    Both his and my function would return 0 in that case.

    You aren't by any chance having a medication-free day again, are you?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Both his and my function would return 0 in that case.

    You aren't by any chance having a medication free day again, are you?
    Ah, I didn't see the && loc == -1. I was thinking his just returned the last match.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Ah, I didn't see the && loc == -1. I was thinking his just returned the last match.
    Uh-huh... I suppose I can can let you off the hook this time ... LOL.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Check my code over.
    By omGeeK in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 11:34 PM
  2. Would you check my code, please?
    By maLio3 in forum C Programming
    Replies: 8
    Last Post: 08-24-2010, 12:39 PM
  3. check this code out
    By abyssphobia in forum C++ Programming
    Replies: 9
    Last Post: 08-15-2004, 07:31 PM
  4. Code Check
    By Ajsan in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 09:01 PM
  5. Can anyone check this code
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 10-06-2001, 02:41 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21