Be a Supporter!

Arrays in C++

  • 1,133 Views
  • 14 Replies
New Topic Respond to this Topic
Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Arrays in C++ 2005-10-14 14:43:33 Reply

Mostly due to the fact I have a teacher that doesnt teach for beans I am stuck writing this program that I have no idea how to take on..... I have tried to write it and sadly I am so lost I can't even stand to look at my sloppy code anymore. So if someone could help me out that would be awesome. I can scan I page of the book with the problem and give you the txt file I have to read the date for the program. Thanks!!


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
Jessii
Jessii
  • Member since: Feb. 10, 2005
  • Offline.
Forum Stats
Member
Level 36
Movie Buff
Response to Arrays in C++ 2005-10-14 15:32:02 Reply

I had that problem when I took VB. My teacher had no personality and I even went to him for help but yeah, it wasn't pretty lol. I can't help ya with the C++ though, sorry.

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Arrays in C++ 2005-10-14 15:38:46 Reply

what exactly are you haven problems with? accessing array items? creating an array in the first place? etc


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Arrays in C++ 2005-10-15 11:04:02 Reply

I can help. What is the problem? What do you have coded so far?

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Arrays in C++ 2005-10-15 11:49:55 Reply

I'll try to give a brief overview.

When you declare an array, you must declare the array with the datatype that each individual element of that array will hold. Let's say we want to create an array for students and their grades. Grades are naturally integers, right?

#include <iostream>
int grades[20];

This would create an array for 20 elements. Now, as any other array system, the keys start at 0 so you can really on proceed up to key number 19 while assigning values.

However, a great thing I love about C++ is that if you forget that an item is an array it will return the first element in that array if you access it without a key. For instance, if we were to retrieve the information in grades[5] it would return the 6th grade entry.

At the same time, if we only selected "grades" for retrieving information with no key it would return the value of the first element, or key 0.

I'm not a master on C++ and I hope I helped. It's been a while since I read up and I hope my information is still valid.

Shuko
Shuko
  • Member since: Dec. 29, 2003
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Arrays in C++ 2005-10-15 14:13:26 Reply

This is just a description of arrays. :( There's no problem or exercise listed in there... what are you supposed to do?


"Men don't grow up; they just grow out."

Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Arrays in C++ 2005-10-16 20:58:29 Reply

Well here is the question:

The history teacher at your school needs help in grading a true/false test. The students id and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFT

Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry:

ABc54301 TFTFTFTT TFTFTFFTTFT (SEE THE BLANK IN THE MIDDLE)

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 4 students. Each correct answer is awarded two points, each wrong answer gets -1 point, and no answer gets 0 points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the grade. Assume the following grade scale: 90%-100%,A; 80%-89.99%,B; 70%-79.99%,C; 60%-69.99%,D; and 0%-59.99%, F.

I hope that can all help because all my code I trashed out of dissapointment in myself...


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Arrays in C++ 2005-10-16 21:00:20 Reply

At 10/15/05 11:49 AM, Pilot-Doofy wrote: I'll try to give a brief overview.

I already know what an array is....

I just don't know how to start with the code. Logically I know how it works just putting it into code is the problem....


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Arrays in C++ 2005-10-16 23:07:03 Reply

At 10/16/05 09:00 PM, Streakydesign wrote: I just don't know how to start with the code. Logically I know how it works just putting it into code is the problem....

Write pseudo-code. It helps with writing the program because you don't have to worry about all of the intricate details of a program, just the generally gist of what needs to be done. This pseudo-code should include all of the for, while loops and the functions that you're going to need, but it does not have to be exactly what the C++ code is going to look like.

Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Arrays in C++ 2005-10-17 08:37:35 Reply

No I need it in C++ because I have to hand in the code....act. it was due Friday so i'm already in enough trouble as it is....


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to Arrays in C++ 2005-10-17 12:18:13 Reply

At 10/17/05 08:37 AM, Streakydesign wrote: No I need it in C++ because I have to hand in the code....act. it was due Friday so i'm already in enough trouble as it is....

yes but using pseudo-code will help you to write the code in c++ kind of how a plan helps you to write an essay


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Arrays in C++ 2005-10-17 13:22:21 Reply

First thing I would do is set up a structure to hold student data:

typedef struct
{
char ID[8]; /* The 8 characters of the student's ID */
charAnswers[20]; /* The 20 answers */
int Score; /* The Total score */
char Grade; /* The grade from that score */
} student;

Then, you could write up a few functions to handle the data.
like:

void ReadID( char* ID ); /* Read the student's ID from file */
void ReadAnswers( char* Answers ); /* Read the student's answers from file */
int TotalScore( char* Answers ); /* Total up the score */
char GetGrade( int Score ); /* Get the character grade from the integer score */

void ReadStudent( student* Student ); /* Read all of the student's info */
void DisplayInfo( student* Student ); /* Display all of the studen'ts info */

In the main function, you would create an array of students OR have one student structure and process that data one at a time. Because you don't know the number of students (only that its greater than 4), you should process each student one at a time.
Below is an example of the code:

int main( void )
{
/* Create a student structure */
student Student = {0};
/* Open a file here */

/* Loop while there in no EOF */
do
{
ReadStudent( &Student );
WriteStudent( &Student );
} while( getchar() != EOF ); /* This getchar() also reads newline characters between students for you */

return 0;
}

As far as the files are concerned, since only one file is being used, and it is a relatively shourt program, you can make the file pointer a global. This way you don't need to pass it as a argument to your funcitons.

ReadStudent would look something like this:

void ReadStudent( student* Student )
{
ReadID( Student->ID );
getc( g_File ); /* g_File is just your global file. Read the space in between ID and answers */
ReadAnswers( Student->Answers );
Student-> Score = TotalScore( Student->Answers );
Student->Grade = GetGrade( Student->Score );
}

I think that structuring your program into small pieces like this will make it easier to write, read, and generally understand.

If you need the actual working code for the program, or for one of the functions in particular, let me know.

Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Arrays in C++ 2005-10-17 20:32:01 Reply

now i am more lost......

I hate programming sometimes...

esp. when you go to the teacher for help (today) and he says well maybe you should check out mary kay... It was a joke but.....

Anyways with that code you lost me even more hardcore.. Can someone just type up the code for me without the steps and i'll input the text file that the program needs to input the students info.

It also doesn't need to be all fancy and in depth. Just a simple easy code is all I ask for so I can understand. I'm still new to this ya know...


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
Streakydesign
Streakydesign
  • Member since: Mar. 10, 2000
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Arrays in C++ 2005-10-31 11:00:17 Reply

So anyways it's been how long and i'm still stuck with the same issue and I have 7 programs to write now....

So here it goes whoever is good with c++ and is willing to help me out I would be forever greatful!

Otherwise i'm askind DrLava for help.


Made with 100% Awesomeness | Work in Progress: White Chalk Lotus

BBS Signature
FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Arrays in C++ 2005-11-01 22:48:59 Reply

Well, here it is.
It reads the input from a file called INPUT.TXT
It outputs to the screen.
It was written entirely in ANSI C (not C++) so a few tweaks may be needed.
Your welcome.

/* Program that reads test scores and ID's from a file then prints the
results to the screen *?

/* Includes */
#include <stdio.h>

/* Defines */
#define ID_LENGTH 8
#define QUESTIONS 20

/* Structures */

/* Student */
struct student
{
char ID[ID_LENGTH+1]; /* The 8 characters of the student's ID */
char Answers[QUESTIONS+1]; /* The 20 answers */
int Score; /* The Total score */
char Grade; /* The grade from that score */

};
typedef struct student student;

/* Function Prototypes */
void ReadID( char *ID ); /* Read the student's ID from file */
void ReadAnswers( char *Answers ); /* Read the student's answers from file */
int TotalScore( char *Answers, char *Key ); /* Total up the score */
char GetGrade( int Score ); /* Get the character grade from the integer score */
void ReadStudent( student *Student, char *Key );
void WriteStudent( student *Student );
void ClearStudent( student *Student );

/* Globals */
FILE *g_File;

/* MAIN */
int main ( void )
{
char Key[QUESTIONS] = {0};
/* Create a student structure */
student Student = {0};
/* Open a file here */
g_File = fopen( "INPUT.TXT", "r" );
/* Read the key */
ReadAnswers( Key );
/* Loop while there in no EOF */
do
{
ClearStudent( &Student );
ReadStudent( &Student, Key );
WriteStudent( &Student );
} while( getc( g_File ) != EOF ); /* This getchar() also reads newline characters between students for you */

return 0;
}

void ReadStudent( student *Student, char* Key )
{
ReadID( Student->ID );
getc( g_File ); /* g_File is just your global file. Read the space in between ID and answers */
ReadAnswers( Student->Answers );
Student->Score = TotalScore( Student->Answers, Key );
Student->Grade = GetGrade( Student->Score );
}

void WriteStudent( student *Student )
{
printf( "%s %s %d %c\n", Student->ID, Student->Answers, Student->Score,
Student->Grade );
}

void ReadID( char *ID )
{
/* Declare Variables */
int c = 0, i = 0;
/* First, Read whitespace thats not a number */
c = getc( g_File );
while( c < '0' || c > '9' ) c = getc( g_File );
ID[0] = c;
for( i = 1; i < ID_LENGTH; ++i ) ID[i] = getc( g_File );
}

void ReadAnswers( char *Answers )
{
int i = 0;
for( i = 0; i < QUESTIONS; ++i )
Answers[i] = getc( g_File );
}

int TotalScore( char *Answers, char *Key )
{
int i = 0, score = 0;
/* Loop through all of the questions */
for( i = 0; i < QUESTIONS; ++i )
if( Answers[i] != ' ' )
if( Answers[i] == Key[i] ) score++;
else score--;
return score;
}

char GetGrade( int Score )
{
if( Score >= 18 ) return 'A';
else if( Score >= 16 ) return 'B';
else if( Score >= 14 ) return 'C';
else if( Score >= 12 ) return 'D';
else return 'F';
}

void ClearStudent( student *Student )
{
int i = 0;
Student->Grade = 0;
Student->Score = 0;
for( i = 0; i < ID_LENGTH+1; ++i ) Student->ID[i] = 0;
for( i = 0; i < QUESTIONS+1; ++i ) Student->Answers[i] = 0;
}