Thread: C homework

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    C homework

    hi
    I'm new here. I've a problem with my homework.

    the task is:

    "Write a Windows C program reading student records from a file “records.txt” (given to you). Each line in the file consists of a name (no more than 20 characters), followed by a date of birth in the format dd.mm.yyyy, itself followed by a sequence of tab-separated numerical grades between 1.0 and 6.0. The average grade (in alphabetic format: A, B, C, etc…) for each student must be computed.
    Then a histogram of the average grades must be displayed in the program window (the conversion between numerical grades and alphabetic ones is as specified in session 1 of this course). Also, the average and the standard deviation over all numerical grades has to be computed, displayed, and written to a file “results.txt”. The number of students and the number of grades per student are a priori unknown (so linked lists are a must here…!)."

    Code:
    // definition file for any Windows application
    #include <windows.h>
    // definition file for I/Os
    #include <stdio.h>
    
    #define STR_LEN  20
    #define NOF_ALPHA_GRADES 7
    
    typedef struct grade
    {
     float value;
     struct grade *next;
    } GRADE;
    typedef struct student
    {
     GRADE *first;
     struct student *next;
    } STUDENT;
    // prototype of the call back function for this application (see below)
    LRESULT CALLBACK CBFunc(HWND, UINT, WPARAM, LPARAM);
    // main function of a Windows application
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
     static TCHAR szAppName[] = TEXT("ex13-2");
     HWND hwnd;
     WNDCLASS wndclass;
     MSG msg;
    
     wndclass.style = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc = CBFunc; // "install" the call back function
     wndclass.cbClsExtra = 0;
     wndclass.cbWndExtra = 0;
     wndclass.hInstance = hInstance;
     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
     wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
     wndclass.lpszMenuName = NULL;
     wndclass.lpszClassName = szAppName;
     if (!RegisterClass(&wndclass))
     {
      MessageBox(NULL, TEXT("Class not registered"), szAppName, MB_ICONERROR);
      return(0);
     }
     hwnd = CreateWindow(szAppName, TEXT("ex13-2"), WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
          NULL, NULL, hInstance, NULL);
     ShowWindow(hwnd, iCmdShow);
     UpdateWindow(hwnd);
     // endless message dispatching loop
     while (GetMessage(&msg, NULL, 0, 0))
     {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
     }
     return(msg.wParam);
    }
    
    STUDENT *ReadLine(FILE *fptr)
    {
     char str1[STR_LEN + 1];
     char str2[STR_LEN + 1];
     float f;
     STUDENT *sptr;
     GRADE *gptr;
     // read past the name and the date of birth
     if (fscanf(fptr, "%s%s", str1, str2) == 0) return(NULL);
     // reserve memory for a new student
     sptr = malloc(sizeof(STUDENT));
     if (sptr == NULL) return(NULL);
     // initialize the new student's attributes
     sptr->first = NULL;
     sptr->next = NULL;
     // read the grades
     while (fscanf(fptr, "%f", &f) != 0)
     {
      // reserve memory for a new grade
      gptr = malloc(sizeof(GRADE));
      if (gptr == NULL) return(sptr);
      // set the new grade's attributes
      gptr->value = f;
      gptr->next = sptr->first;
      sptr->first = gptr;
     }
     return(sptr);
    }
    
    STUDENT *ReadInputFile(char *filename)
    {
     FILE *fptr;
     STUDENT *sptr, *first = NULL;
     fptr = fopen(filename, "r");
     if (fptr == NULL) return(NULL);
     while ((sptr = ReadLine(fptr)) != NULL)
     {
      sptr->next = first;
      first = sptr;
     }
     fclose(fptr);
     return(first);
    }
    
    void ComputeStats(STUDENT *firsts, int histogram[], float *avrg_addr, float *sd_addr)
    {
     GRADE *gptr;
     float avrg;
     float tot_avrg = 0.0;
     int count;
     int tot_count = 0;
     int index;
     float sd = 0.0;
     while (firsts != NULL)
     {
      avrg = 0.0;
      count = 0;
      gptr = firsts->first;
      while (gptr != NULL)
      {
       avrg += gptr->value;
       count++;
       gptr = gptr->next;
      }
      if (count != 0)
      {
       tot_avrg += avrg;
       tot_count += count;
       avrg /= count;
       index = ConvertNumGrade(avrg);
       histogram[index]++;
      }
      firsts = firsts->next;
     }
     // compute the standard deviation
     if (tot_count != 0)
     {
      tot_avrg /= tot_count;
      ???
     }
     ???
    }
    
    void DispStats(int histogram[], float avrg, float sd)
    {
    }
    
    // definition of the call back function, i.e. the function which gets automatically called
    // by the OS whenever a Windows event (msg) occurs for the application
    LRESULT CALLBACK CBFunc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
     HDC hdc;
     PAINTSTRUCT ps;
     static STUDENT *first = NULL;
     static int histogram[NOF_ALPHA_GRADES] = {0, 0, 0, 0, 0, 0, 0};
     static float avrg = 0.0;
     static float sd = 0.0;
     switch(msg)
     {
      case WM_SIZE:
       // read the input file (only) at the beginning of the program
       if (first == NULL)
       {
        first = ReadInputFile("records.txt");
       }
       ComputeStats(first, histogram, &avrg, &sd);
       DispStats(histogram, avrg, sd);
       return(0);
      case WM_PAINT:
       // do something
       return(0);
      // other "case" statements according to needs
      case WM_DESTROY:
       PostQuitMessage(0);
       return(0);
     }
     return(DefWindowProc(hwnd, msg, wParam, lParam));
    }
    As you see, I don't have to write the whole code. I've only to fill the place with the question marks.

    but the first problem is, i don't understuand what means "standard deviation".
    And the second problem is, I don't know how to write this code.

    know some user what's the solution?
    Last edited by bringit; 12-17-2011 at 12:31 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well you found this site so you probably know how to use a search engine...
    What went so horribly wrong when you tried to do a search on "standard deviation", or what prevented you from doing so?
    It kinda seems like the most logical first step.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can somebody help me with my homework in c++
    By gosse in forum C++ Programming
    Replies: 19
    Last Post: 04-29-2011, 02:10 PM
  2. help about homework
    By agathery in forum C Programming
    Replies: 27
    Last Post: 05-19-2010, 09:17 PM
  3. Homework Help!!
    By Jdub in forum C Programming
    Replies: 10
    Last Post: 04-24-2010, 02:39 AM
  4. homework
    By misplaced in forum C++ Programming
    Replies: 18
    Last Post: 10-04-2004, 06:56 AM
  5. I want homework!
    By Tynnhammar in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 02:49 PM