Thread: Help printing location?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Help printing location?

    need help im reading in a file that has a list

    0 2
    1 4
    0 2
    1 2

    so if there is a one in the first column it will tell me the location of my first max. So my first max is location 2 and i need it to print to the screen. I am completly lost on how to do this any help?

    Code:
    /* Number 10 */
    
    #include<stdio.h>
    #include<math.h>
    #define FILENAME "exam1.dat"
    
    main()
    {
    
    /* Define Variables*/
       int n_d_p=0,col_1,col_2, locmax, i;
       double sum=0.0,max,min,ave,flag_0=0,flag_1=0;
       FILE *list1;
    
    /* Opening External File */
       list1=fopen(FILENAME,"r");
    
    /* Reading the numbers in opened file */
    
       while((fscanf(list1,"%i %i",&col_1,&col_2))==2)
    {
    
         /* Computing The Average */
          if(col_1==1)
         {
           sum +=col_2;
           n_d_p++;
    
    
        /* Max Calcualtion */
          if(col_1==1)
          {
            if(flag_1==0)
          {
             flag_1=1;
             max=col_2;
           }
         if(col_2>max)max=col_2;
            }
    }
    }
    
    /* First Max *********NEED HELP HERE******/
       if(col_1==1)
    
    
    
    
    
    ave=sum/n_d_p;
    
    printf("Average: %7.2f \n",ave);
    printf("Maximum: %4i \n",max);
    printf("Loc of first max: %4i \n",locmax);
    
    fclose(list1);
    
    /*exit program*/
    return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure exactly what it is you're trying to do. What do you mean by "So my first max is location 2 and i need it to print to the screen."

    What do you mean? If it's something else, are you sending it to a printer or a file? Or do you want to put it at a specific part of the screen? Or what exactly?


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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Yes I want it to print to the screen. So for example.

    0 2
    1 4
    0 2
    1 2

    When it reads the list if there is a 0 in the first column it does nothing with number next to it. But when i reach my first 1 in col_1 I need it to print out to the screen and tell me that the number four is at location 2. So print out to the screen schould be something like this

    Location of first max: 2

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do
        read a line from file
        if read was successful
            increment line counter
            if first number is 1
                print the line number
    while not at end of file
    There's the logic. Try turning it into code. Post what you can't do or get stuck on.


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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What you've posted is that there must be a 1 in the first column to tell you the location of your first max. Now imagine you are the program, and you have read in the first two numbers:

    0 2

    There is no 1 in the left column yet, so I'm guessing that your English is not great, and what you mean is "my first max is the larger number of the two I've read in from the file, so far".

    Since 0 < 2, 2 is your max number, so far. When your program reads:
    1 4,
    then since 4 > your previous max number, you new max number becomes 4.

    Is that right, or have I misunderstood what you posted?

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Sorry I was in a physics class while typing this up and never proof read it. But close. The list exam1.dat file im reading in is

    0 2
    1 4
    0 6
    0 2
    1 4
    1 4

    If there is a 0 in the first column it does nothing with the adjacent integer. What my problem is, I need to print to the screen the "location" of the first maximum, not the actual number 4.

    So the print out should be
    loc of first max: 2

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've already explained how to do it.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 09:27 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM