Lots of little questions

This is a discussion on Lots of little questions within the C Programming forums, part of the General Programming Boards category; Hi all, I have a couple of problems, so I thought I'd concentrate them into 1 post: Problem 1: I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    14

    Lots of little questions

    Hi all,

    I have a couple of problems, so I thought I'd concentrate them into 1 post:

    Problem 1:

    I have a text file with a list of numbers like this:

    1
    2
    3
    4
    5
    6
    etc.

    I need to write a program which reads this file, then prints the smallest number, largest number and the mean. I can write the part which reads the file and the part which calculates the mean, but I have no idea how to tell it to work out the smallest and largest number...

    Problem 2:

    I have another text file with several lines of information which look like this:

    a b c d e f g
    h i j k l m n
    etc.

    where each letter in this case is either a word or a number, separated by tabs. The program needs to prompt the user to enter a line number, and then the program will extract the data from that line, and print it in a readable way. I don't know how to do this, so I was wondering if someone could either tell me how, or at least point me in the right direction with a function which can scan certain lines...

    Thanks very much in advance for the aid.

    Cheers,

    RayJay.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    In your case, "problem" is really "assignment" spelled wrong. We don't do your homework for you.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I can write the part which reads the file and the part which calculates the mean, but I have no idea how to tell it to work out the smallest and largest number...
    That's the easiest part.
    Code:
    int smallest = MAX_INT;
    int largest = 0;
    for each number in file
        if number < smallest
            smallest = number;
        if number > largest
            largest = number
    I'm not sure what your problem is with #2. Is it the part where you need to skip to the nth line in the file?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    Quote Originally Posted by Dino View Post
    In your case, "problem" is really "assignment" spelled wrong. We don't do your homework for you.
    Except it's not - it's for my job (I'm not even at school mate) - so if you don't have a constructive answer, please don't reply...

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    Quote Originally Posted by bithub View Post
    I'm not sure what your problem is with #2. Is it the part where you need to skip to the nth line in the file?
    Yer - that's the bit which I'm tied up on - skipping to the nth line of the file - i can use scanf to scan for the tab separated values, but it's the inputting of the line which I'm stuck on

    Thanks for the help with issue 1!

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This should be enough to get you started.
    Code:
    int line_counter = 0;
    char buffer[100];
    while(fgets(buffer,sizeof(buffer),f)) {
        if(buffer[strlen(buffer)-1] == '\n')
            line_counter++; // we reached the end of a line
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by RayJay View Post
    Except it's not - it's for my job (I'm not even at school mate) - so if you don't have a constructive answer, please don't reply...
    You're getting paid to write a program to find the biggest number in a file?

    /facepalm

    I should work where you work.


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

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    For Problem 1, Google "bubble sort" or some sorting algorithm
    For Problem 2, store each line in an array of structs, whose lone member is a big enough buffer. Now print the requested line by printing the [line-1]th member of the array.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by quzah View Post
    /facepalm

    I should work where you work.


    Quzah.
    Me too! lol

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    Quote Originally Posted by itCbitC View Post
    Me too! lol
    Haha - I'm not getting paid to do - it's just to make my paid job easier... like VBA!

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    14
    Quote Originally Posted by bithub View Post
    This should be enough to get you started.
    Code:
    int line_counter = 0;
    char buffer[100];
    while(fgets(buffer,sizeof(buffer),f)) {
        if(buffer[strlen(buffer)-1] == '\n')
            line_counter++; // we reached the end of a line
    }
    Thanks very much man! You're a star!

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by itCbitC View Post
    For Problem 1, Google "bubble sort" or some sorting algorithm
    For Problem 2, store each line in an array of structs, whose lone member is a big enough buffer. Now print the requested line by printing the [line-1]th member of the array.
    Why would he need bubble sort (or any other sorting algorithm) for this?
    Your "suggestion" for problem 2 is equally confusing. Why would he need to store every line in an array? How do you ensure that your buffer is "big enough"?
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    What a bunch of bull......... It's flying lately, now that we're at the end of the semester.

    Enjoy your steaming cup of fail come exam time.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by bithub View Post
    Why would he need bubble sort (or any other sorting algorithm) for this?
    Just my 2c as I skimped the post albeit your method is simpler 'n better.
    Quote Originally Posted by bithub View Post
    Your "suggestion" for problem 2 is equally confusing. Why would he need to store every line in an array?
    Not for a one-time request but if the program looped asking multiple times for different lines - albeit resource constraints, if any, would come into play.
    Quote Originally Posted by bithub View Post
    How do you ensure that your buffer is "big enough"?
    Same buffer size as your suggestion would suffice for all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Really new & Really clueless (lots of different questions)
    By Tigerfeet in forum C++ Programming
    Replies: 7
    Last Post: 11-07-2008, 03:32 PM
  2. Two questions. (Sorry for lots of them =( )
    By ozumsafa in forum C Programming
    Replies: 10
    Last Post: 09-21-2007, 03:57 AM
  3. Lots of questions
    By belhifet in forum C# Programming
    Replies: 5
    Last Post: 12-20-2006, 06:39 PM
  4. Lots Of Bitmap Texture Questions:
    By Krak in forum Game Programming
    Replies: 7
    Last Post: 07-10-2003, 08:16 PM
  5. Lots of RPG questions...
    By Kyoto Oshiro in forum Game Programming
    Replies: 22
    Last Post: 07-21-2002, 10:47 PM

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