Thread: Help getting started :(

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    Help getting started :(

    I need a little help getting started on an assignment that is due later in the week. I'm not asking y'all to do it for me, just a little help with the design of the program. Any tips are greatly appreciated. Thanks in advance for those who reply. Here is the general outline for the project:

    Code:
    Create a C program that performs the following:
    
    Open an input file, "test.txt" (no quotes in the filename itself), and if 
    successful, an output file, "test.out". If opening either file is
    unsuccessful, an appropriate error message should be printed to screen
    and the program should not execute any more tasks. You may not use exit() 
    or abort(). Use selection to avoid executing. If the input file is opened 
    successfully, it will not have errors. It will contain a seed value (integer)
    for the pseudorandom number generator and the number of random values to 
    generate (integer). 
    
    The program must keep statistics of how frequently a number is generated
    in the range [1,50] by using an array to count occurrences of each number.
    Once the program has generated all numbers, it should create the output file
    containing a report of the percentage of time a particular number was 
    generated. It should print five values per line as shown below in Example 
    Output. The more random numbers that are generated, the better the
    distribution should become. Remember that your array subscripts start at 0.
    I need to have prototype such as:

    Code:
       bool openFiles(FILE **pFPIn, FILE **pFPOut);
    I started to write the program earlier in the week, then picked it back up again today and became frustrated. I wasnt sure how exactly it should be set up. I'm just looking for general pointers on what to do as nothing I seem to be doing is working at all.

    I was thinking this program would generally work like this.

    Open Input File/Test -> Open Output File/Test -> Read SEED and number of generations -> Generate numbers and store them as an array -> Do the statistics on the array and write to output -> Displaying data in output file.

    Is there an easier way to go about writing this? Can I just do it in two functions y'all think? With main calling the openFiles function where the magic happens?

    I'm honestly stuck and appreciate any tips y'all can provide with this.

    Here is what I had before I came online.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #define SIZE 50
    
    void displayName(void);
    bool openFiles(FILE **pFPIn, FILE **pFPOut); 
    void printResults(int arr[], int qrs);
    
    int main(void) {
    
       int arr[SIZE], qrs;
    
       displayName();
    
    //   openFiles(*FPIn, *FPOut);  ----- doesnt work for now
    
       printResults(arr, qrs);
    
       return 0;
    }
    
    //displays name
    void displayName(void) {
    
       printf("\nName: XXXXXXXXXXX\n\n");
    }
    
    bool openFiles(FILE **pFPIn, FILE **pFPOut) {
    
       FILE *FPIn, *FPOut;
       int cInput;
    
       if ((FPIn = fopen("test", "r")) != NULL) {
       
           if ((FPOut = fopen("out", "w")) != NULL) {
            
              while ((cInput = fgetc(fpIn)) != EOF)
                 fputc(cInput, fpOut);
              fclose(fpOut);
           }
           else
              printf("\nUnable to open \"out\" for writing.");
           fclose(fpIn);
       }
        else
           printf("\nUnable to open \"test\" for reading.");
    
    
    }
    
    
    // Just testing out arrays here and getting the column number down
    void printResults(int arr[], int qrs)   {
       
       
    
        for (qrs = 0; qrs < SIZE; qrs++)
           arr[qrs] = qrs * 5;
          
       for (qrs = 0; qrs < SIZE; qrs++)
        {
           if (qrs % 5 == 0)
              printf("\n");
           printf("%4d", arr[qrs]);
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    That's the one I was studying earlier today (playing with mine and looking at his/hers), but my main focus on help is how the program should be set up.

    Thanks for the reply!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd take this:
    was thinking this program would generally work like this.

    Open Input File/Test -> Open Output File/Test -> Read SEED and number of generations -> Generate numbers and store them as an array -> Do the statistics on the array and write to output -> Displaying data in output file.
    and a big yellow pad of paper (because there are VERY few problems that can't be solved with a pen/penicl and a big yellow pad of paper) , and now just "flesh" it out for your functions:

    int main - of course
    int OpenFile
    - - test for success opening the file(s).
    - - return value perhaps 0 for success, return 1 for succesful output of error text, and return 2 for no file success at all.
    if success opening good file, read random seed and generations #.
    Assign same to a struct whose address you have passed to OpenFile()

    else if filestatus == 1 then handle error text file
    end

    return to main with result

    In main(), (flip back to the main() page in your pad.)
    if ( status is 0 ) then fill array with random numbers and call Stats() with the address of the array Stats(int random_array[]) (which is just an address, not the whole array, but we can use it)

    Stats() - do your statistics and counting, etc., here, and output to file

    Keep fleshing out the functions above, and PUT OFF THE DETAILS. We don't care MUCH about 'dem dam details, now!

    Every page of your yellow pad get's a new function (this is a law, so don't break it!) and just keep adding in pseudo-code until you can see how the DESIGN of your program, will indeed work. BIG picture here - no details, yet.

    Then just take your pseudo-code, and translate it into C. You've worked from the top-down to design it, now you'll work from the bottom up to implement all the details you ignored earlier. But you'll find it easy with a good design - that's the key to this.

    That's the easy way to design a program. Tough way is to just sit down at the keyboard and start typing in code. That can be a real witch!

    Adak

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Thanks, no big yellow pad

    Big white pad though!

    Thanks~^^, once I finish the program (hopefully sooner than later) I'll post it for all of you to see!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 09:51 PM
  2. Getting started
    By panzeriti in forum Game Programming
    Replies: 3
    Last Post: 06-28-2003, 10:13 AM
  3. Getting Started
    By UnclePunker in forum Game Programming
    Replies: 3
    Last Post: 04-11-2003, 12:34 PM
  4. How to get started?
    By anoopks in forum Linux Programming
    Replies: 0
    Last Post: 01-14-2003, 03:48 AM
  5. Need help getting started
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2001, 11:08 PM