Thread: Need advice on small detail of my programming project

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    Need advice on small detail of my programming project

    Hello. I am new to the forum but I hope i'll be very active since i'm starting with my programming classes and i'll need all the help I can get

    Anyway I have this project:

    Write a project that reads in five integers and then determines and prints the largest and the smallest integers in the group. The program must readthe data from an external file and print the results to another external file. USE LOOPING.

    And here's what I came up with

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(int argc, char *argv[])
    {  int a, b, c, d, e, smallest, largest; FILE *numbers; FILE *results;
        
        
        numbers = fopen("data.txt","r");
        results = fopen ("results.txt","w");
        
    
            fscanf(numbers, "%d %d %d %d %d", &a, &b, &c, &d, &e );
        
        smallest = a;
        if (b < smallest)
            smallest = b;
        if (c < smallest)
            smallest = c;
        if (d < smallest)
            smallest = d;
        if (e < smallest)
            smallest = e;
        
           fprintf ( results, "smallest is %d\n", smallest);
           
        largest = a;
        if (b > largest)
            largest = b;
        if (c >largest)
            largest = c;
        if (d >largest)
            largest = d;
        if (e > largest)
            largest = e;
            
            fprintf ( results, "largest is %d\n", largest);
           
           
           while ( fscanf (numbers, "%d", &a, &b, &c, &d, &e)!= EOF )    
        
        fclose(results);
        fclose(numbers);
            
        
      
      
      return 0;
    }

    The problem is that when I conferred with the prof. he said that it was fine and dandy but then he asked(with an evil grin(BASTAGE THAT HE IS BTW )) :
    What if you had 5 million integers to sort out? Would you want to write 5 million "ifs"? I want only ONE if for the smallest and only ONE if for the largest for the program to sort the integers out into smallest and largest.

    So thats my problem, I can't for the life of me come up with a looping set of code that uses only ONE if to sort the integers into largest and smallest.

    ANY help will be greatly appreciated.

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Let's assume you have an integer array. Create two integers min and max and set their value to the first element in the array. Now loop through the array. If the value of the current index in the array is less than min, set min to the current value. If the value of the current index in the array is greater than max, set max to the current value.

    There you go. One loop to find the min and max.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Wow, umm I kinda understand what you are saying but... You see I'm taking my FIRST programming and I don't know what an array is and how to loop through it.

    I'm assuming the array is the list of numbers i have on the external file I have to call up for the data? How do I loop through the numbers so the program can check to see if the are bigger or smaller than the set min and max?


    Lemme check my book and see if I can come up with something similar to what youre saying. Also the prof. specified that we can't use stuff that we haven't covered in class and I don't think we have covered arrays... Also I can't use ANY CODE WHATSOEVER from C++. The prof. said to use while statements. Still I'm if you could post an example of the code you would use for the array I'd appreciate it a lot. i'm checking in my book to see if I can come up with something.

    *EDIT*
    Nope it seems I can't use arrays since that is material for my final exam... Damn, I actually looked it up and solving my problem would be SOOOO easy if I could use an array but I can't. Anyone have any ideas?
    Last edited by tibu; 09-21-2004 at 09:14 PM.

  4. #4
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Can you use strtok?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    There are some sorting algorithms at www.aihorizon.com that deal with efficiency and basic methods, etc... but if you don't understand arrays or if you're not 100% comfortable with arrays yet, I'd definately recommend that you work a little bit on that for this project.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    People! Why arrays? It's much simplier than that:
    1. read 1 integer
    2. set it's value to both largest and smallest
    3. set n to 1
    4. while (n < NUMBERS_YOU_HAVE_TO_READ)
    {
    5. read 1 integer
    6. compare it with largest and smallest, and correct them if needed
    7. increment n
    }
    8. write output

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Ok, what does read 1 integer mean? Sorry but I don't think everyone realizes that this is the SECOND program I have ever done by myself so I am very very noobe at this. iwabee thanks for the help im trying to come up with something based on what you posted right now.

    *edit*
    I have no idea how to integrate what iwabee wrote into the program I have right now. Especially since the numbers I have to sort out, I have to obtain from an external file called data.txt
    Last edited by tibu; 09-21-2004 at 09:54 PM.

  8. #8
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    It's hard to determine how much you've already covered because this assignment seems pretty intermediate in the sense that you should already know what a pointer is (since you're using a FILE pointer) and reading and writing to file streams, but you don't know what an array is.

    Now one way to do this is to use fgets to save the contents to a string and then use strtok() to obtain each number (token) in the string. You then use atoi() to convert the token into an integer so that you use the algorithm I listed to obtain the min and max.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Right now the only commands I know of(as in covered in class) are printf, scanf, While, do while, or, if, fscanf, fprintf, and thats about it. Well not counting the counters, and arithmetic commands.

    I'm pretty stumped. Guess I'll have no other option than to hand it tomorrow the one I posted and let him take off my grades whatever points he wants to. I'll stay up till late trying to find a way...

  10. #10
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    How is this external file formatted?

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    ok, now I understand. You've copy&pasted code for file processing and don't really understand how it works.
    Code:
    this code reads 1 integer:
    fscanf(numbers, "%d", &a);
    
    and this code reads 5 integers at once:
    fscanf(numbers, "%d %d %d %d %d", &a, &b, &c, &d, &e );
    You see the pattern here, right?
    Aww what the hell I'll just write and comment it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBERS_YOU_HAVE_TO_READ 5
    
    int main(void)
    {
      int a, n, smallest, largest;
      FILE *numbers, *results;
      
      numbers = fopen("data.txt","r");
      results = fopen ("results.txt","w");
    
      fscanf(numbers, "%d", &a); // Reading 1 integer from input file
      smallest = largest = a;    // Setting smallest and largest to number readed
      
      n = 1; // Setting n to 1
      while (n < NUMBERS_YOU_HAVE_TO_READ) // Looping while n is less than whatever
      {
        fscanf(numbers, "%d", &a);  // Reading 1 integer from input file
        
        if (a < smallest)           // Comparing it with smallest
          smallest = a;             // and correcting if needed
        if (a > largest)            // Comparing it with largest
          largest = a;              // and correcting if needed
        
        n++; // Incrementing n. Equivalent to n = n + 1;
      }
      
      // Writing output to output file
      fprintf ( results, "smallest is %d\n", smallest);
      fprintf ( results, "largest is %d\n", largest);
      
      // Cleanup
      fclose(results);
      fclose(numbers);
      
      return 0;
    }
    And don't just copypaste it! Write it again and try to understand and memorize.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Well you see my prof gave everyone a special handout with the assignment itself that tells us how to do file processing. He didn't actually cover it in class.

    In teh handout he specified the code for how to read from a file and how to write to a file so I obvously used it. Other than that EVERYTHING ELSE in my program I wrote. So I don't think it's fair to call me up on plagiarism, I'm just working with what the prof. gave me and this forum basically.

    Thanks for the help iwabee, im trying to go through what you wrote and see how I can apply it by myself.

    *edit*
    The file is just a .txt file with 5 numbers like this:

    3 9 5 8 1

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    He'll have to at least figure out why it's only asking for 4 instead of the 5 expected numbers.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Hmmm I have figured it out. If I start out with n=1 and I add 1 to n everytime the loop is run; from the number 1 to the number 5 are only 4 numbers and not 5. Therefore I have to start out with n = 0.

    See? I mean I obviously didn't join the forum for you people to do my work for me. The program I first posted I did with the tools I could remember from class and my book. However I needed a more effcicient way of doing the same task and THAT I needed help with.

    Anywayz thanks to everyone that pitched in. I could possibly get a perfect score now.

  15. #15
    ReMNoRz choochoo machine
    Join Date
    Sep 2004
    Posts
    1


    Nice sig itsme86, I actually had a smirk in my face when I read it... I feel like a nerd right now...

    Anyway don't be so hard on tib... I was helping him you know, and he knows his stuff, he's just a bit lost in a programming world, anyway I couldn't help him (even though I shouldn't have had any problems...)...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  2. How to add a file to a project in bloodshed.
    By Smeep in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 09:29 PM
  3. Convert C++ project
    By gogo in forum Tech Board
    Replies: 6
    Last Post: 08-12-2004, 07:48 AM
  4. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:16 PM
  5. almost done...help with small detail...
    By pancho in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 06:35 AM