Solve Function & Array Exercise for Students

In summary: Marker 1 Marked giving an average of Market 2 Marked giving an average of
  • #1
jnbankwa
14
0
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 realized 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:
// 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:
Physics news on Phys.org
  • #2
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?
 
  • #3
these codes has been written myself but is not working properly
 
  • #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
 
  • #5
jnbankwa said:
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
You need to display what in these lines?

The output you show agrees exactly with your code.
 
  • #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)
 
  • #7
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.
 
  • #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"
 
  • #9
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.
 
  • #10
thanks
 
  • #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.
 
  • #12
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.
 
  • #13
Or you can use sprintf to create the text.

Code:
char ave[80];
sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
Gwin.writeText(10, y, ave);
 
  • #14
dGasim said:
Or you can use sprintf to create the text.

Code:
char ave[80];
sprintf(ave, "Average is %d %f", listing[n].Score.WhoMarked/11, Marks);
Gwin.writeText(10, y, ave);
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.
 
  • #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;
}
 
  • #16
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:
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.
 
  • #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;
 
  • #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)
{
 
  • #19
Did you read what I said in post #16?
jnbankwa said:
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;


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");
 
  • #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;
}
 
  • #21
What's your question? There are syntax errors in this code, so it won't even compile...
 
  • #22
that my main problem and i cannot solve it
 
  • #23
jnbankwa said:
that my main problem and i cannot solve it
What is?

I made these comments a couple of posts ago.
Mark44 said:
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");


Did you understand what I was saying in them?
 
  • #24
I see something else that is undoubtedly causing problems for you. This is from your first post in this thread.
jnbankwa said:
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.
Code:
struct marks
{
int ID [NUMSTUDENTS];
double Score [NUMSTUDENTS]; 
int WhoMarked [NUMSTUDENTS];
int Marker 1[NUMSTUDENTS];
int Marker 2 [NUMSTUDENTS];
1. The name of your struct template should be Assignment. It should hold the information about one student. Your program should define an array whose elements are struct instances. The size of the array should be NUMSTUDENTS. The information in each struct instance should be ID, Score, and WhoMarked.
2. Your struct definition above is incorrect, as it is missing the closing brace and semicolon - };

In post #20 you have this code:
Code:
struct 
{
int marks [NUMSTUDENTS];
int ID [NUMSTUDENTS];
double Score [NUMSTUDENTS]; 
int WhoMarked [NUMSTUDENTS];
};
In this code you removed the struct tag, so now you have a struct template that can't be used. The struct template definition should look like this:
Code:
struct Assignment
{
   int ID;
   double Score; 
   int WhoMarked;
};
You are more familiar with your assignment than I am, but I believe this is what you are being asked to use.
 
  • #25
please tell me what wrong ?
// 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 Assignment
{
int ID;
double Score;
int WhoMarked;
};

Student[NUMSTUDENTS];
double SelAverage;
double average;
int Marker1;
int Marker2;
Assignment *pnt;

void ApplyReheat(Assignment *pnt)int main()
{
GWindow Gwin;
// Clear the Gwin window
Gwin.clear();

Gwin.setPenColour(BLACK);

ifstream myfile("marks.txt");
while (!myfile.eof())
{
myfile >> summaryN12345[count].Score >> summaryN12345[count].Whomarked >> summaryN12345[count].ID;
count++;
}
myfile.close();
count=0;

Gwin.writeText(0,0,"N0362030: ");
SelAverage = Gwin.readInt();
for (int i=0;i<NUMSTUDENTS;i++)
{
if (SelAverage == marks.ID)
{
pnt = &Student;
ApplyReheat(pnt);
}
}

Gwin.writeText(10,10,"Marker 1 Marked giving an average of ");
Gwin.writeText(10,30,"Market 2 Marked giving an average of ");
for (int i=0;i<NUMSTUDENTS;i++)
{
if (selAverage == music.Genre)
{
average=((marks.Score*11/music.ID);
Gwin.writeText(0,40+(20*count),marks.Whomatked);
Gwin.writeInt(150,40+(20*count),average);
Gwin.writeDouble(220,40+(20*count),marks.Score*11);
count++;
Marker1 = Markers1 + average;
markers2 = Marker2 + (average*marks.ID);
}
}
Keyboard.waitKey();

return 0;
}
 
  • #26
jnbankwa said:
please tell me what wrong ?
What's wrong is that you are showing code here that won't even compile, let alone run, due to numerous typos and undeclared variables. You should try to compile this code, and fix each of the syntax errors the compiler notices. When your code compiles without errors, then we can see about getting it to run correctly.

Is this assignment for a class you are taking? Are there prerequisites for this class? The level of this assignment and the work you are showing suggests to me that you aren't quite ready for this class. If I were having this much trouble with an assignment, I would consider dropping the class and looking for a class that was a better match with my capabilities.
 
  • #27
I've done it and i will have a new one this afternoon and I will post it of the forum if I stuck
 

1. How can I improve my understanding of functions and arrays?

To improve your understanding of functions and arrays, it is important to practice solving exercises and problems regularly. This will help you become more familiar with the concepts and their applications. Additionally, you can research online resources or seek help from a tutor or mentor.

2. What are some common mistakes students make when solving function and array exercises?

Some common mistakes students make when solving function and array exercises include not properly declaring variables, not using the correct syntax for functions or loops, and not considering edge cases. It is important to carefully read the instructions and double-check your code for errors.

3. How can I effectively debug my code when solving function and array exercises?

To effectively debug your code, you can use a debugger tool or logging statements to track the values of your variables and identify any errors. Another helpful strategy is to break down your code into smaller parts and test each part individually.

4. Can you provide some tips for optimizing my code when solving function and array exercises?

To optimize your code, you can use built-in functions or methods instead of writing lengthy code, avoid unnecessary loops or conditions, and use proper variable naming conventions. It is also helpful to plan and pseudocode your solution before writing the actual code.

5. How can I apply my knowledge of functions and arrays to real-world problems?

Functions and arrays are fundamental concepts in computer science and are widely used in various industries. To apply your knowledge to real-world problems, you can practice solving coding challenges and participate in coding competitions. You can also work on personal projects or contribute to open-source projects to gain practical experience.

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top