Begginers excercises

Pages: 12
i have also done the last task for the pancackes it took some time but i got it done with a little help from google :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
	cout << " enter name : ";
	cin >> Names[0];
	cout << "How many pancakes you eat today ";
	cin >> Number_Eaten[0];

	for (int i=1; i<PAN; i++)
	{	
		cout << " enter name : ";
		cin >> Names[i];
		cout << "How many pancakes you eat today ";
		cin >> Number_Eaten[i];
	}

	for (int start = 0; start < PAN; start++)
	{
		int largest = start;
		for (int Current = start + 1; Current < PAN; Current++)
		{
			if (Number_Eaten[Current] > Number_Eaten[largest])
			{
				largest = Current;
			}
		} 

		swap(Number_Eaten[start], Number_Eaten[largest]);
		cout << Number_Eaten[start];
	}
	cin.get();
	cin.get();

	return 0;
}


however i couldnt seem display the right names with the right pancake numbers they inputted :(
here is the third task of the bracket searching programme :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
srand( time(NULL) );

	guess = rand() % 100 + 1;

	cout << " type in a umber that  i will guess ";
	cin >> user;

	while (true)
	{
		cout << " is it: " << guess << endl;

                guess = rand() % 100 + 1;

		cout << " type 0 if correct , 1 if high or 2 if low ";
		cin >> response;		

		if (response ==0)
		{
			cout << "yay i guessed it";
			break;
		}

		if (response ==1)
		{
			cout << " number high" << endl;
		}

		else if (response ==2)
		{
			cout << " number low" << endl;
		}
		count++;
	}


EDIT : the last task took me a little time but here it is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 srand( time(NULL) );

	guess = rand() % 100 + 1;

	cout << " type in a umber that  i will guess ";
	cin >> user;

	while (true)
	{
		cout << " is it: " << guess << endl;
		
		guess = rand() % 100 + 1;

		cout << " type 0 if correct , 1 if high or 2 if low ";
		cin >> response;


		if (response ==0)
		{
			cout << "yay i guessed it";
			break;
		}

		if (response ==1)
		{
			cout << " number high" << endl;
			max = guess - 1;
			guess = (max+min)/2;

		}

		else if (response ==2)
		{
			cout << " number low" << endl;
			min = guess + 1;
			guess = (max+min)/2;
		}
		count++;
	}
Last edited on
here is the first task with fun with functions :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void say_hello();

int main()
{

	say_hello();

	cin.get();

	return 0;
}

void say_hello()
{
	cout << " Hello ";
}


here is the 2nd task of fun with functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void say_hello(int number);

int main()
{
	int number =0;

	cout << "type in number of hellos to output";
	cin >> number;
	say_hello(number);

	cin.get();
	cin.get();

	return 0;
}

void say_hello(int number)
{
	for (int i =0; i<number; i++)
	{
		cout << " Hello " << endl;
	}
}


here is the third task with fun with functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int multiplication(int x, int y);

int main()
{
	int y=0;
	int x=0;

	cout << "type in the first value to multiply " ;
	cin >> x;
	cout << "type in the second value ";
	cin >> y;
	
	cout << multiplication(x, y);

	cin.get();
	cin.get();

	return 0;
}

int multiplication(int x, int y)
{
	int answer = x*y;
	
	return answer;
}


here is the third task :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void half(int number);

int main()
{
	int number = 0;

	cout << " please type in number to half ";
	cin >> number;

	half(number);

	cin.get();
	cin.get();

	return 0;
}

void half(int number)
{
	cout << number<< endl;
	int newn =0;
	int half =0;
	newn = number/2;
	cout << newn << endl;

	if (newn>0)
	{	
		half = newn/2;
		cout << half << endl;
	}

	if (newn<0)
	{
		cout << "exit";
	}
}


i have tried to do this task but it isnt complete, i have done this code to what i thought is right but i dont understand on how to pass the new number from the function to the function again , can any1 point me to the right direction it is much appreciated

here is the actual problem :
★★★ Make a function called half() that takes an integer argument. The function must print the number it received to the screen, then the program should divide that number by two to make a new number. If the new number is greater than zero the function then calls the function half() passing it the new number as its argument. If the number is zero or less than the function exits

Call the function half() with an argument of 100, the screen output should be
100
50
25
...
...
1.
Last edited on
hello all , i have done the first task of tic tac toe game but i think its a bit tedious can any1 tell me if there a better way of going about this i couldnt think of a different way of doing it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const int TAC = 3;
int Tic_array[TAC][TAC] = 
{
	{1,2,3},
	{4,5,6},
	{7,8,9}
};

int Tac_plyr1[3];
int Tac_plyr2[3];


int main()
{
	int player1=0;
	int player2=0;

	cout << "choose the number on the grid to place 0 or x; \n ";

	for (int i=0; i<TAC; i++)
	{
		for (int a=0; a<TAC; a++)
		{
			cout << Tic_array[i][a] << " ";	
		}
		cout << endl;
	}

	for (int e=0; e<TAC; e++)
	{
		cin >> Tac_plyr1[e];
		cin >> Tac_plyr2[e];

		if ((Tac_plyr1[0] == 1 ) && (Tac_plyr1[1] == 5) && (Tac_plyr1[2] == 9))
		{
			cout << " congratulations player 1 wins ";break;
		}

		if ((Tac_plyr2[0] == 1 ) && (Tac_plyr2[1] == 5) && (Tac_plyr2[2] == 9))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 1 ) && (Tac_plyr1[1] == 4) && (Tac_plyr1[2] == 7))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 1 ) && (Tac_plyr2[1] == 4) && (Tac_plyr2[2] == 7))
		{
		cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 2 ) && (Tac_plyr1[1] == 5) && (Tac_plyr1[2] == 8))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 2 ) && (Tac_plyr2[1] == 5) && (Tac_plyr2[2] == 8))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 3 ) && (Tac_plyr1[1] == 6) && (Tac_plyr1[2] == 9))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 3 ) && (Tac_plyr2[1] == 6) && (Tac_plyr2[2] == 9))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 1 ) && (Tac_plyr1[1] == 2) && (Tac_plyr1[2] == 3))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 1 ) && (Tac_plyr2[1] == 2) && (Tac_plyr2[2] == 3))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 4 ) && (Tac_plyr1[1] == 5) && (Tac_plyr1[2] == 6))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 4 ) && (Tac_plyr2[1] == 5) && (Tac_plyr2[2] == 6))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 7 ) && (Tac_plyr1[1] == 8) && (Tac_plyr1[2] == 9))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 7 ) && (Tac_plyr2[1] == 8) && (Tac_plyr2[2] == 9))
		{
			cout << " congratulations player 2 wins ";break;
		}
		if ((Tac_plyr1[0] == 3 ) && (Tac_plyr1[1] == 5) && (Tac_plyr1[2] == 7))
		{
			cout << " congratulations player 1 wins ";break;
		}
		if ((Tac_plyr2[0] == 3 ) && (Tac_plyr2[1] == 5) && (Tac_plyr2[2] == 7))
		{
			cout << " congratulations player 2 wins ";break;
		}
	}
		cin.get();
		cin.get();

		return 0;
}


iwell i could but didnt know how to implement it i kept getting stuck :(
Last edited on
Hi there,

I recently did the same commenting for someone else's tic tac toe game.
You can find it here, some of it applies to your code too:

http://cplusplus.com/forum/beginner/75571/

Hope that helps.
All the best,
NwN
Hey! I'm doing the same thing! Wanna be study C++ buddy!? :D
thnx for the reply and link NwN and hey why not DetectiveRawr :)
here is the first part of the strings excercise :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int _tmain(int argc, _TCHAR* argv[])
{
	char firstname[256];
	char surname[256];

	cout << " please enter first name \n ";
	cin.getline(firstname, 256);
	cout << " enter surname please \n ";
	cin.getline(surname, 256);

	cout << firstname << " " << surname;
	cin.get();
	cin.get();
	return 0;
}
Some one HELP............. i have to create a program which inputs the number of lines and creates this triangle

"n=5
15
14 13
12 11 10
9 8 7 6
5 4 3 2 1
"

Dude HELP!!!!!!!!!!!!!!!!!!!
hey i have done the second task for string functions :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int _tmain(int argc, _TCHAR* argv[])
{
	char firstname[256];
	char surname[256];

	cout << " please enter first name \n ";
	cin.getline(firstname, 256);
	cout << " enter surname please \n ";
	cin.getline(surname, 256);
	string f = firstname;
	string s = surname;

	replace( f.begin(), f.end(), 'a' , 'z');
	replace( f.begin(), f.end(), 'e' , 'z');
	replace( f.begin(), f.end(), 'i' , 'z');
	replace( f.begin(), f.end(), 'o' , 'z');
	replace( f.begin(), f.end(), 'u' , 'z');

	replace( s.begin(), s.end(), 'a' , 'z');
	replace( s.begin(), s.end(), 'e' , 'z');
	replace( s.begin(), s.end(), 'i' , 'z');
	replace( s.begin(), s.end(), 'o' , 'z');
	replace( s.begin(), s.end(), 'u' , 'z');

	cout << f << " " << s;

	cin.get();
	cin.get();
	return 0;
}


however i feel i can reduce the lines using a different function i will get back to u guys on that :)

heres an improved version :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int _tmain(int argc, _TCHAR* argv[])
{
	char firstname[256];
	char surname[256];
	char found;
	char found1;

	cout << " please enter first name \n ";
	cin.getline(firstname, 256);
	cout << " enter surname please \n ";
	cin.getline(surname, 256);

	string f = firstname;
	string s = surname;

	found = f.find_first_of("aeiou");
	found1 = s.find_first_of("aeiou");

	while(found!=string::npos)
	{
		f[found]='z';
		found=f.find_first_of("aeiou",found+1);

		s[found1]='z';
		found1=s.find_first_of("aeiou",found1+1);
	}

	cout << f << " " << s;

	cin.get();
	cin.get();
	return 0;
}


although most help came from looking at your string class tutorial :)

EDIT:
i have done the last task for string funtions :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 int _tmain(int argc, _TCHAR* argv[])
{
	char firstname[256];
	char surname[256];

	cout << " please enter first name \n ";
	cin.getline(firstname, 256);
	cout << " enter surname please \n ";
	cin.getline(surname, 256);

	string f = firstname;
	string s = surname;

	string::reverse_iterator name;

	for ( name=s.rbegin() ; name < s.rend(); name++ )
	{
		cout << *name;
	}
	
	cout << " ";

	for ( name=f.rbegin() ; name < f.rend(); name++ )
	{
		cout << *name;
	}

	cin.get();
	cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12