Hey I'm new, need help with a project

This is a discussion on Hey I'm new, need help with a project within the C Programming forums, part of the General Programming Boards category; I've attached the project details in a word document. I'm trying to write the solution algorithm only in pseudo code ...

  1. #1
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15

    Hey I'm new, need help with a project

    I've attached the project details in a word document.

    I'm trying to write the solution algorithm only in pseudo code at the moment before I write the actual code but am pretty stuck. I know I need to read the file then store the information into an array(s) but not sure what the arrays should be and how I should collate the data etc. Any help you can give me will be greatly appreciated thank you!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There isn't much computation involved. It's read the data, put it into the array, and write it out in the format that has been chosen (a report).

    Not a lot to pseudo code, imo.

    A struct with members to hold each field of each record, is a normal way to go. Then you make an array of the structs you've got, and load the array up with data.

    You'll need an input function, perhaps a function to arrange or compute some data as you want or need it to be, and then an output function.

    Post your code for this, and let us know what you're stuck on.

    If you can't figure out "How do I do this part?", just grab a paper and pencil and do it by hand. Now list the steps you took to do it by hand, in the most simple and efficient way. There's the start to your pseudo code, right there.

  3. #3
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Ok so I have it in a structure... I'm unsure of how to compute the data. There's 36 different groups in the report and the only thing I can think of right now is using an If...else statement but that would make 36 different options. Is there anything else I could do for doing that?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Do you have any current code to post?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by drty2 View Post
    Ok so I have it in a structure... I'm unsure of how to compute the data. There's 36 different groups in the report and the only thing I can think of right now is using an If...else statement but that would make 36 different options. Is there anything else I could do for doing that?
    You could have 17 groups of 2 options

    Seriously tho: if you mean is there something that will mean less tedious typing for me, consider it's only 36 repeatable lines. If you mean is there a more "optimal" way to do this (which may mean more, less, or the same amount of typing) then if you can break the options into sets of probabilities (I don't deal with .doc, so I don't know what these groups are), then you can reduce the number of if statements that actually must be processed in a single event cycle -- so it would never have to be more than 4, rather than a high of 36 and a mean of 17.

    The relevancy of that depends on the frequency and number of repeated cycles, but of course if the point is educational then practice.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We need to see a sample of the input data before we can say how it should be handled.

    Normally you have a struct with things like:

    name
    sex
    age
    zip code
    type of customer
    lead
    etc.

    Then it doesn't matter about "groups", because everybody "fits" into the struct - they all have a name, a sex, an age, etc.

    They ALL go into the array, and we manipulate what we want on the report, and where, from there.

    Please post your code, and some sample input. The doctors must see the patient to make a diagnosis.

  7. #7
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Ok I just started to write some code and so far have:

    Code:
    #include <stdio.h>
    
    void readfile();
    
    void main()
    {
       readfile();
    }
    
    void readfile()
    {
    
       struct record
       {
          char gender;
          int age;
          int lead;
       };
    
       FILE *fpin;
    
       fpin = fopen("custsurvey.dat", "r");
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
    but am unsure how to read the records from the file into the struct. All the searches I've done I've only come up with how to store the data from a user entry.

    The data in the file is formatted like this

    M 65 4
    F 20 2
    F 32 5
    M 12 1
    etc.
    Last edited by drty2; 01-14-2009 at 09:08 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need a function to return a line from the file, probably using read() or fgetc(). read() works with file descriptors and not streams, altho you can map one to the other. After that you want to parse the line with strtok() or sscanf().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by drty2 View Post
    Ok I just started to write some code and so far have:

    Code:
    #include <stdio.h>
    
    void readfile();
    
    void main()
    {
       readfile();
    }
    
    void readfile()
    {
    
       struct record
       {
          char gender;
          int age;
          int lead;
       };
    
       FILE *fpin;
    
       fpin = fopen("custsurvey.dat", "r");
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
    but am unsure how to read the records from the file into the struct. All the searches I've done I've only come up with how to store the data from a user entry.

    The data in the file is formatted like this

    M 65 4
    F 20 2
    F 32 5
    M 12 1
    etc.
    I would suggest moving the struct definition to main() (and please make it "int main()", not "void main()". Add a "return 0;" statement at the end of int main(). (for technical reasons I won't go into). Then make or declare a struct (at present you have only defined a struct, but you have none declared (or created)). Then pass a pointer to readfile, and read each record in. You'll need to move the file pointer to main() also.

    Code:
    #define MaxRecords 4  //up near the very tip top of the program
    
    struct records rec[MaxRecords]; //in main()
    
    //part of readfile()
    ok = 1;
    while(ok > 0)  {
       for(i = 0; i < MaxRecords; i++)  {
          ok = fscanf("%c", rec[i].sex);
    
          if(!ok)
             break;   //add for all three fscanf's
    
          ok = fscanf("%d", rec[i].age);
          ok = fscanf("%d", rec[i].lead);
       }
    }
    Using fgets() is excellent, as well, but you will need a buffer to hold it temporarily, and then you'll need to get your data from the buffer - I just think this is more in tune with a raw beginner.

    The code I'm putting up is not tested - use it for idea's, and know it won't be perfect.

  10. #10
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Would this be ok?

    Code:
    #include <stdio.h>
    #define MAXRECORDS 4
    
    struct record
    {
       char gender;
       int age;
       int lead;
    };
    
    void readfile();
    
    void main()
    {
    
       struct records rec[MAXRECORDS];
       readfile();
    }
    
    void readfile()
    {
       FILE *fpin;
    
       fpin = fopen("custsurvey.dat", "r");
    
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
          while(!feof(fpin))
          {
             for(i = 0; i < MAXRECORDS; i++)
                fscanf(fpin, "%c  %d %d", &rec[i].gender, &rec[i].age, &rec[i].lead));
          }
       }
    
       fclose(fpin);
    }
    And also how would I pass the pointer to the function? Just unsure how I would with the code I have.
    Last edited by drty2; 01-14-2009 at 10:55 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that looks about right. Does it work when you try it out?

    A good principle in programming is to write a bit of code, try it out, write a bit more, try it out, fix it, try it out, write a bit more code, etc, etc.

    The more advanced form of this is described here:
    http://www.agiledata.org/essays/tdd.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    I don't have a compiler at home so haven't been able to test the code yet.

    So now say that it does work and all the information is in the struct. How would I collate the data for the report?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several sources of compilers - getting one so you can try code out at home is almost essential.

    Code::Blocks is an IDE (editor and compiler package) that you can get together with gcc-mingw. Doesn't cost anything more than a bit of downloading.

    Or, if you don't mind quite a large download, Microsoft Visual Studio C++ Express Edition is available at no cost from Microsoft themselves.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Never code without one
    Nor code without an editor or IDE of kind!
    But I have to remind you that you failed to change "void main" to "int main".
    Also, the records are stored inside main. How do you think you can use them inside readfile in this case?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Help please!
    By black_hole??? in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2009, 12:55 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. MFC in an empty project
    By cunnus88 in forum Windows Programming
    Replies: 0
    Last Post: 10-09-2005, 09:19 AM
  4. 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
  5. Hey, check out this project...
    By bluehead in forum C++ Programming
    Replies: 8
    Last Post: 11-07-2001, 05:13 PM

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