[Uni test] Sep 30 2008

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[Uni test] Sep 30 2008

Post by ayu »

We had a small test today, where (as always) the teacher comes running into the computer lab and yells

"YOU HAVE 45 MINUTES TO DO THIS TEST, IF YOU CAN'T DO IT IN THAT TIME, YOU WILL BE KICKED OUT OF THE GROUP!"

Ok, maybe I spiced up the story a bit... but that's what he said ^^ (or was it get punched in the face?)...

Anyway!

Here it is... the 15 minute stress test code

http://code.suck-o.com/84

Code: Select all

#include <iostream>
#include <cstring>
#include <string>

#define for(int i=0; i<NumPers; ++i) NumLoop

using namespace std;

int main()
{
	int NumPers, num;
	string name[20][20], month[12] = { "January", 
										"February", 
										"Mars", 
										"April", 
										"Maj", 
										"Juni", 
										"Juli", 
										"Augusti", 
										"September", 
										"Oktober", 
										"November", 
										"December" };

	

	cout << "Specify number of persons:";
	cin >> NumPers;
	cin.ignore();

	NumLoop
	{
		cout << "Specify name number " << i+1 << ":";
		getline(cin, name[i][0]);

		cout << "Specify birth month:";
		getline(cin, name[i][1]);
	}

	

	NumLoop
	{

		num = atoi( name[i][1].c_str() ) - 1;
		cout << name[i][0] << " was born in " << month[num] << "\n";
	}
	

	return 0;

}
"The best place to hide a tree, is in a forest"

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

remove that defined... it is useless

I don't really get why you have name[20][20]
It looks like you could use name[20][2]

and why you use name[0] ?

and if numpers is bigger than 20, BOOM
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Don't double post :lol:

remove that defined... it is useless
It's not useless, so I will not remove it.... I think it fits in perfectly =)
I don't really get why you have name[20][20]
Test description demanded it
and why you use name[0] ?

and if numpers is bigger than 20, BOOM


Because I had to get the value in position 0 for the current name. And the assignment wanted 20 positions.

Remember, this was a 45 minute test under stress, just make it work and turn it in as fast as possible ^^
"The best place to hide a tree, is in a forest"

Post Reply