Thread: Learning arrays

  1. #1
    Unregistered
    Guest

    Learning arrays

    I'm writing a program to read numbers from a file and then print out the largest and second largest numbers in that file.
    What am I doing wrong? Thanks for the help.
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 50

    int main()
    {

    int values[SIZE],
    largest,
    second_largest,
    newest;
    int count = 0;
    int i;
    FILE *input_file_ptr;
    input_file_ptr = fopen("data.c","r");
    if (input_file_ptr == NULL)
    {
    printf("The data file couldn't be opened.\n");
    return 0;
    }
    else /*read the file into the array*/
    {
    while (fscanf(input_file_ptr;"%d", &newest)==1)
    {
    values[count] = newest;
    count++;
    }
    }
    /*search for the largest element*/

    switch(count)
    {
    case 0: printf("No largest or second largest.");
    break;
    case 1: printf("The largest number is %d.\n",values[0]);
    printf("There is not a second largest.\n");
    break;

    case 2: if (values[0]< values[1])
    {
    printf("Largest is %d, second largest is %d.",
    values[1],values[0]);
    break;
    }
    else (values[0]> values[1])
    {
    printf("Largest is %d, second largest is %d.",
    values[0],values[1]);
    break;
    }

    default: break;/*cases larger than 2*/
    }

    largest = values[0];
    second_largest = values[0];

    for (i=1; i < 50; i++)
    {
    { if (values[i]>largest)
    largest = values[i];
    else if (values[i]== largest)
    largest = second_largest;
    else if (values[i]> second_largest)
    second_largest = values[i];
    }

    if (largest == second_largest)
    {
    printf("The largest and second largest are the same");
    printf(" number: %d.\n",largest);
    }
    else if (largest != second_largest)
    {
    printf("The largest number is: %d.\n",largest);
    printf("The second largest number is: %d.\n",second_largest);
    }
    }
    return 0;

    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your two bugs are here:
    >while (fscanf(input_file_ptr;"%d", &newest)==1)

    And here:
    >else (values[0]> values[1])

    The first bug is easily fixed by changing the semicolon after input_file_ptr to a comma and the second by either removing the test or changing the else to an else if.

    Personally, I think you're making this way too complicated. Just keep two survivor variables and at the end of the program, they will contain the largest and second largest values:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int largest = 0,
          nextLargest = 0,
          rawVal;
      char buffer[BUFSIZ];
      printf ( "Enter a series of integers (CTRL+Z to quit)\n$ " );
      while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
        if ( sscanf ( buffer, "%d", &rawVal ) == 1 ) {
          if ( rawVal > largest ) {
            nextLargest = largest;
            largest = rawVal;
          }
          else if ( rawVal > nextLargest )
            nextLargest = rawVal;
        }
        else
          (void)puts ( "Invalid integer" );
        printf ( "$ " );
      }
      printf ( "The largest value is: %d\n"
               "The next largest is:  %d\n", largest, nextLargest );
      return 0;
    }
    This can easily be converted to read from a file instead of standard input.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    What is the error that you are getting?

    I can see that you have put a semi colon after the first argument to fscanf. This should be a comma - so your code wont compile right there.
    Code:
    while (fscanf(input_file_ptr;"%d", &newest)==1)
    And.. the else should be an "else if" if you wish to evaluate another statement. Making these changes should allow it to compile, I haven't actually checked the logic though.

    Code:
    else (values[0]> values[1])
    Oh, I think there are a couple of unnecassary brackets in there as well - that may cause some compilation errors. I think one of them was in this block of code.

    Code:
    for (i=1; i < 50; i++) 
    { 
    { if (values[i]>largest) 
    	  largest = values[i]; 
      else if (values[i]== largest) 
      	largest = second_largest; 
      else if (values[i]> second_largest) 
      	second_largest = values[i]; 
    }
    Also - please use the code tags when posting code, it makes it easier to read
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ahh.. Damn - I think you can type faster than I can Prelude.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Unregistered
    Guest
    Thanks for the help guys. My logic is messed up but at least it is compiling now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  3. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM