1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Function and array

  1. Aug 25, 2011 #1
    Dear please read this exercise and the codes and tells me what wrong with you codes

    Part 1
    You are provided with a skeleton program, where the data is stored in arrays. Your task is to add the code that will do the following:
    Examine the data in the arrays. The data shows the results from marking a set of student assignments. The arrays contain the Marker who was assigned this piece of work (set to 1 for the first person marking the students and 2 for the other person marking the students), the student Id, and the Mark awarded. (Your program has to count how many students were marked by each marker in order to calculate the averages correctly.) Your program must then display the two averages and how many students each marker marked.
    The results should look as below:
    Part 2
    Change your program so that it no longer uses sets of arrays, but instead declares a struct called Assignment for each student and uses a single array of these. Your program must read the information in from a file called marks.txt (which is already supplied in the folder), which contains the same information as the arrays.
    You should produce the same display, because the information is the same.
    Your program must also write the summary results as shown on screen to another file called “summaryN12345.txt” where N12345 is your NTU ID.
    Part 3
    During the moderation process, it has been realised that marker 1 and marker 2 have very different averages. To correct this you must write a function called Moderate which has a void return and a single parameter of a pointer or reference to the struct Assignment. This function must be called for every assignment in the array. If the parameter denotes an Assignment marked by marker 1, the body of the function must increase the mark by 20% of its current value (ie a mark of 50 would be increased by 20% of its value to 60). Your display must now show the effect of these changes:
    Code (Text):

    // ExtendedtestA.cpp : Defines the entry point for the application.
    // Author:
    // ID:
    // Version:
    // Date: 6 Jun 2011
    // Description:

    #include "stdafx.h"
    #include "gwin.h"

    using namespace std;
    const int NUMSTUDENTS = 11;

    int ID [NUMSTUDENTS] = {10001, 10011, 10012, 1004, 1005, 1006, 1007, 1008, 1009, 1010,1011};
    double Score [NUMSTUDENTS] = {47.5, 62.0, 34.5, 59.0, 68.0, 42.5, 81.5, 36.5, 55.0, 63.5, 45.8};
    int WhoMarked [NUMSTUDENTS] = {2,1,1,2,2,1,2,1,1,2,1};


    struct marks
    {
    int ID [NUMSTUDENTS];
    double Score [NUMSTUDENTS];
    int WhoMarked [NUMSTUDENTS];
    int Marker 1[NUMSTUDENTS];
    int Marker 2 [NUMSTUDENTS];

    void ApplyReheat(marks*marks)
    {
    marks->Score=marks->Score/11;
    }

    int main()
    {
        GWindow Gwin;
    Marker 1 Marked[NUMSTUDENTS];
    Marker 2 Marked[NUMSTUDENTS];
    ifstream marks("marks.txt");
    int n=0;
    while (!marks.eof())
    {
    marksfile >>listing[n].ID;
    marksfile >>listing[n]. Score;
    marksfile >>listing[n]. WhoMarked;
    n++;
    if (n== NUMSTUDENTS)
        break;
    }

        Gwin.clear();
       
        Gwin.setPenColour(BLACK);
       
        Gwin.writeText(10,10,"Marker 1 Marked                   giving an average of                    ");
        Gwin.writeText(10,30,"Market 2 Marked                   giving an average of                    ");
    int count=0;
        int y=20;
        double twr=0;
        int Average=0;
    double Marks=0
        while (count< NUMSTUDENTS)
        {
            ApplyReheat (&listing[count]);
            Gwin.writeText(10,y,listing[count].Score);
            Gwin.writeInt(50,y,listing[count].WhoMarked);
    Average=(listing[count].ID*listing[count].Score)/listing[count]. WhoMarked;

            Gwin.writeDouble(100,y,twr);
            if (twr > Marks)
            {
                Marks=twr;
                Average=count;
            }
            if (twr>1)
                Gwin.writeText(150,y,"X");


            count++;
            y+=10;
            if (count== NUMSTUDENTS)
                break;

        }

        Gwin.writeText(10,y,"Average is :");
        Gwin.writeText(200,y, listing[n]. Score. WhoMarked/11);
        Gwin.writeDouble(250,y,Marks);
        Keyboard.waitKey();
       
        return 0;
    }
     
     
    Last edited by a moderator: Aug 25, 2011
  2. jcsd
  3. Aug 25, 2011 #2

    berkeman

    User Avatar

    Staff: Mentor

    Welcome to the PF.

    I added code tags to your code for readability. What *exactly* is your question? Have you added code as described in the problem statement? What have you done so far?
     
  4. Aug 25, 2011 #3
    these codes has been written myself but is not working properly
     
  5. Aug 25, 2011 #4
    I need to display in Gwin.writeText(10,10,"Marker 1 Marked giving an average of ");
    Gwin.writeText(10,30,"Market 2 Marked giving an average of ");

    Marker 1 Marked giving an average of
    Market 2 Marked giving an average of
     
  6. Aug 25, 2011 #5

    Mark44

    Staff: Mentor

    You need to display what in these lines?

    The output you show agrees exactly with your code.
     
  7. Aug 26, 2011 #6
    Dear from the exercises written in 3 part I have done until the last part which I have to display in one line a Marker 1 Marked ( Mark) giving an average of ( average) and in the second line the same Market 2 Marked ( mark) giving an average of ( average) , unfortunately It display only Marker 1 Marked ( empty) giving an average of ( empty) and in the second line the same Market 2 Marked ( empty) giving an average of (empty)
     
  8. Aug 26, 2011 #7

    Mark44

    Staff: Mentor

    Look in your Gwin.h header file and see if there is a member function named write in the gwin class. You are using the writeText function, which I believe is to be used just for displaying text. If you are unsure of what to do, show us the gwin.h header file.
     
  9. Aug 28, 2011 #8
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //

    #pragma once

    #include <iostream>

    #include "gwin.h"
    #pragma once

    #include "resource.h"
     
  10. Aug 28, 2011 #9

    Mark44

    Staff: Mentor

    Your program is using a method that is declared in gwin.h. You need to open this header (gwin.h) with an editor and look to see what other functions are declared in it.
     
  11. Aug 28, 2011 #10
    thanks
     
  12. Aug 28, 2011 #11
    If you want to use a textbook example and convert it to Gwin, the rule is:

    if the textbook says

    cout<<X;

    you look at the TYPE of the variable X, and replace this line by a call to

    Gwin.writeInt(X);

    or

    Gwin.writeDouble(X);

    or

    Gwin.writeText(X);

    depending on what the type was int or double or char[] eg "Hello"

    If you see a line that says:

    cin>>Y;

    you look at the TYPE of the variable Y, and replace this line by a call to

    Y=Gwin.readInt();

    or

    Y=Gwin.readDouble();

    or

    Y=Gwin.readChar();

    depending on whether the type was int or double or char (eg 'Q' - a single character)

    There are obviously other functions you can call in Gwin to make your program more interesting, but the control of it will be based on the rest of your C++.

    At the moment you are writing small programs, so you are declaring only a few variables, and using fairly simple structures eg a simple while loop and a few if tests to decide what to do. So the Gwin bits of it seem to be a relatively large part of the program. As your programs get bigger, you will spend more time writing control and variables in basic C++ and less of your program will involve input and output to the screen.
     
  13. Aug 28, 2011 #12

    Mark44

    Staff: Mentor

    That's the information you need. To get your program to produce the output you want, use writeInt to display an integer value, and use writeDouble to display a double value. The writeText function you are using in your program should be used only to display text.
     
  14. Aug 28, 2011 #13
    Or you can use sprintf to create the text.

    Code (Text):

    char ave[80];
    sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
    Gwin.writeText(10, y, ave);
     
     
  15. Aug 29, 2011 #14

    Mark44

    Staff: Mentor

    I believe that the purpose of the exercise is to use the member functions of the gwin class, rather than using C-style standard library functions.
     
  16. Aug 29, 2011 #15
    the new program is below but the same problem

    // ExtendedtestA.cpp : Defines the entry point for the application.
    // Author:
    // ID:
    // Version:
    // Date: 6 Jun 2011
    // Description:

    #include "stdafx.h"
    #include "gwin.h"

    using namespace std;
    const int NUMSTUDENTS = 11;

    /*int ID [NUMSTUDENTS] = {10001, 10011, 10012, 1004, 1005, 1006, 1007, 1008, 1009, 1010,1011};
    double Score [NUMSTUDENTS] = {47.5, 62.0, 34.5, 59.0, 68.0, 42.5, 81.5, 36.5, 55.0, 63.5, 45.8};
    int WhoMarked [NUMSTUDENTS] = {2,1,1,2,2,1,2,1,1,2,1};
    */


    struct marks
    {
    int ID [NUMSTUDENTS];
    double Score [NUMSTUDENTS];
    int WhoMarked [NUMSTUDENTS];
    };
    void ApplyReheat(marks*mark);
    {


    int main()
    {
    GWindow Gwin;
    ifstream file("marks.txt");
    // Clear the Gwin window
    double ID=0;
    int y=20;
    int i=0;
    double Score=-1;
    int Whomarked;
    char most;
    Gwin.clear();
    marks marks[NUMSTUDENTS];
    Gwin.setPenColour(BLACK);
    Gwin.writeInt(0,0,"Marker marked giving an average of ");
    Gwin.writeInt(10,0,"Marker marked giving an average of ");

    while (i<7)
    {
    file>>marks.marks>>marks.ID>>marks.Score>>marks.Whomarked;
    Gwin.writeInt(25,y,marks.marks);
    Gwin.writeInt(150,y,marks.Score);
    ApplyReheat(&marks);
    weight=marks.Score*marks.Whomarked;
    weight=weight/marks.Score;
    Gwin.writeDouble(250,y,weight);
    if (weight>=1)
    {
    Gwin.writeInt(320,y,"X");
    }

    if (Whomarked<=weight)
    {
    Score=weight;
    average=marks.ID;

    }

    y=y+20;
    i++;
    }

    char ave[80];
    sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
    Gwin.writeText(10, y, ave);

    // Finally, wait for a key to be pressed
    Keyboard.waitKey();

    return 0;
    }

    void ApplyReheat(marks*mark)
    {
    mark->Whomarked=mark->Whomarked/11;
    }
     
  17. Aug 29, 2011 #16

    Mark44

    Staff: Mentor

    I'm almost certain that you aren't using the output functions correctly. I don't know how they are supposed to be used, since you have not included a listing of gwin.h in your posts.

    I am certain that you aren't using the writeInt function correctly, based on these lines in your code:
    Code (Text):
    Gwin.writeInt(0,0,"Marker marked giving an average of ");
    Gwin.writeInt(10,0,"Marker marked giving an average of ");
     
    Based on its name, the writeInt function should be used to display an integer value, not display some text. To output some text and a numeric value you will need to use writeText and either writeInt or writeDouble.

    The declarations of writeInt, writeText, and writeDouble in gwin.h provide information about the number of parameters to use when you call them, and the types of these parameters.
     
  18. Aug 29, 2011 #17
    it still not working

    GWindow Gwin;
    ifstream file("marks.txt");
    // Clear the Gwin window
    double ID=0;
    int y=20;
    int i=0;
    double Score=-1;
    int Whomarked;
    char most;
    Gwin.clear();
    marks marks[NUMSTUDENTS];
    Gwin.setPenColour(BLACK);
    char ave[80];
    sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
    Gwin.writeText(10, y, ave);
    Gwin.writeInt(0,0,"Marker marked giving an average of ");
    Gwin.writeInt(10,0,"Marker marked giving an average of ");

    while (i<7)
    {
    file>>marks.marks>>marks.ID>>marks.Score>>marks.Whomarked;
    Gwin.writeInt(25,y,marks.marks);
    Gwin.writeInt(150,y,marks.Score);
    ApplyReheat(&marks);
    weight=marks.Score*marks.Whomarked;
    weight=weight/marks.Score;
    Gwin.writeDouble(250,y,weight);
    if (weight>=1)
    {
    Gwin.writeInt(320,y,"X");
    }

    if (Whomarked<=weight)
    {
    Score=weight;
    average=marks.ID;
     
  19. Aug 29, 2011 #18
    Gwin.clear();
    marks marks[NUMSTUDENTS];
    Gwin.setPenColour(BLACK);
    char ave[80];
    sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
    Gwin.writeText(10, y, ave);
    Gwin.writeInt(0,0,"Marker marked giving an average of ");
    Gwin.writeInt(10,0,"Marker marked giving an average of ");

    while (i<7)
    {
    file>>marks.marks>>marks.ID>>marks.Score>>marks.Whomarked;
    Gwin.writeInt(25,y,marks.marks);
    Gwin.writeInt(150,y,marks.Score);
    ApplyReheat(&marks);
    weight=marks.Score*marks.Whomarked;
    weight=weight/marks.Score;
    Gwin.writeDouble(250,y,weight);
    if (weight>=1)
    {
    Gwin.writeInt(320,y,"X");
    }

    if (Whomarked<=weight)
    {
     
  20. Aug 29, 2011 #19

    Mark44

    Staff: Mentor

    Did you read what I said in post #16?


    Some comments:
    1. This is very confusing to me: marks marks[NUMSTUDENTS];
    You have an array named marks that is of type marks. You should not have the array name being the same as the type of data that is in the array.
    2. I don't see that you are opening the file "marks.txt". You have created a variable of type ifstream (named file), but you aren't actually opening the file.
    3. You should not be using sprintf. All of the display functions you need are declared in gwin.h. Your instructor will probably deduct points for using sprintf.
    4. You are using writeInt to display a string of characters. That's not what it's for.
    5. Your struct does not have a member named marks, so this line is incorrect:
    Gwin.writeInt(25,y,marks.marks);
    What are the names of the members of your struct?
    6. The Score member of your struct is of type double, so you should not be using writeInt to display one of these values.
    Gwin.writeInt(150,y,marks.Score);
    7. writeInt should not be used to display a string of characters, as in this line:
    Gwin.writeInt(320,y,"X");
     
  21. Aug 29, 2011 #20
    // ExtendedtestA.cpp : Defines the entry point for the application.
    // Author:
    // ID:
    // Version:
    // Date: 6 Jun 2011
    // Description:

    #include "stdafx.h"
    #include "gwin.h"

    using namespace std;
    const int NUMSTUDENTS = 11;

    /*int ID [NUMSTUDENTS] = {10001, 10011, 10012, 1004, 1005, 1006, 1007, 1008, 1009, 1010,1011};
    double Score [NUMSTUDENTS] = {47.5, 62.0, 34.5, 59.0, 68.0, 42.5, 81.5, 36.5, 55.0, 63.5, 45.8};
    int WhoMarked [NUMSTUDENTS] = {2,1,1,2,2,1,2,1,1,2,1};
    */


    struct
    {
    int marks [NUMSTUDENTS];
    int ID [NUMSTUDENTS];
    double Score [NUMSTUDENTS];
    int WhoMarked [NUMSTUDENTS];
    };
    void ApplyReheat(marks*mark);
    {


    int main()
    {
    GWindow Gwin;
    ifstream file("marks.txt");
    // Clear the Gwin window
    double ID=0;
    int y=20;
    int i=0;
    double Score=-1;
    int Whomarked;
    char most;
    Gwin.clear();
    Marker1[NUMSTUDENTS];
    Marker2[NUMSTUDENTS];
    Gwin.setPenColour(BLACK);
    int average=0;

    Gwin.writeText(10, y, ave);
    Gwin.writeInt(0,0,"Marker marked giving an average of ");
    Gwin.writeInt(10,0,"Marker marked giving an average of ");

    while (i<7)
    {
    file>>marks.marks>>marks.ID>>marks.Score>>marks.Whomarked;
    Gwin.writeDoule(25,y,marks.marks);
    Gwin.writeDoule(150,y,marks.Score);
    ApplyReheat(&marks);
    average=marks.Score*marks.Whomarked;
    average=Score/marks.Score;
    Gwin.writeDouble(250,y,weight);
    if (average>=1)
    {
    Gwin.writeDoule(320,y,"X");
    }

    if (Whomarked<=weight)
    {
    Score=weight;
    average=marks.ID;

    }

    y=y+20;
    i++;
    }



    // Finally, wait for a key to be pressed
    Keyboard.waitKey();

    return 0;
    }

    void ApplyReheat(marks*mark)
    {
    mark->Whomarked=mark->Whomarked/11;
    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Function and array
  1. Confused with array (Replies: 3)

  2. Array C++ (Replies: 2)

  3. C - array (Replies: 2)

Loading...