Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C Programming Challeging question..Please Help!! T.T

nested loop

  • Please log in to reply
3 replies to this topic

#1 SamTheGreat

SamTheGreat

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 October 2010 - 10:12 AM

Hi experts,

This is my college project..i have tried it a lot of times but compilation keeps failing and i have run out of idea...please somebody if u see this..please give a helping hand...i appreciate very very much if someone could help...the question is as follows:

• Define a structure with the following data members:
• studid (use char array of 10 elements to store the Student ID)
• lectatt (use int array of 14 elements to store 14 weeks of lecture attendance).
• Use the preprocessor directive to define constants for elements in both arrays.

• In main() function :
• Declare an array of structure variable mystudents with 7 elements. Use the preprocessor directive to define the constant for this array also.
• Declare a pointer to a file called input_file.
• Declare and initialize the following arrays of variable.
o hours_attended (total hours attended for each students)
o att_percentage (the percentage of attendance based on the assumption that students need to attend 2 hours of lecture every week)
• Use the fopen() function call to open a file named attendance.dat for read only and assign the return value the pointer of the input_file.
• Check whether the attendance.dat can be open or not.
• If the file can be opened, use a nested for loop the insert data in the array of structure mystudents.
o Use the outer for loop to read the Student ID and insert the data into the structure array.
o Use the inner for loop to read the attendance where each number represent the number of hours attended.
• Close the input file.
• Create another nested for loop just like the first nested loop to get the total hours attended and get the percentage (assuming that students need to attend 2 hours every week for 14 weeks).
• Use the last for loop to display the Student ID and the attendance percentage for each student.

Thanking you in advanced, and best regards,
C programming Student
  • 0

#2 SamTheGreat

SamTheGreat

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 October 2010 - 11:16 AM

pls help...
  • 0

#3 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 07 October 2010 - 12:22 PM

struct structure_name {
    char data1;
    int data2;
};

int main() {
    struct structure_name my_structure;    // instance of structure_name structure
    my_structure.data2 = 42;                   // you access elements of structure with dot operator
}
This is how structures are defined: start with keyword struct then it's name and inside curly bracets place members/properties/data... Don't forget that structures end with semicolon!
#define SOME_CONSTANT 5
#define MAX_ELEMENTS 10

int array[MAX_ELEMENTS];
This is how you define constants with preprocessor directive.

FILE* pFile;  // file pointer
pFile = fopen("rename_me.txt", attributes);  // first argument is file name to open, second are arguments
                                                                // arguments are "r" for read only, "w" for write and create new, "a" to append
                                                                // and "rb" to read binary files, "wb" write binary files

fprintf(pFile, "Hello, my name is %s\n", "insert-your-name-here");   // writing to file in a printf style

fclose(pFile);    // don't forget to close the file
2 useful functions for binary files are fread and fwrite
  • 0

The roots of education are bitter, but the fruit is sweet.


#4 SamTheGreat

SamTheGreat

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 16 October 2010 - 06:52 AM

Thanks alot flying dutchman, your advise helps alot..
I've figured out the solution and get it done on time..
Thanks alot!

Best and Warmest Regards,
SAM
  • 0





Also tagged with one or more of these keywords: nested loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download