some basic problems with C++

Hi..a student studying c++, exactly just attending c++ courses for some unavoidable cases..I hadn't even learned about c.
.
could anybody help with the answers of the sheet
I know nothing about it but i guess it wouldn't be a hardwork for the ones
who knows c++ a little.
i would really appreciate plz....



TRUE/FALSE questions

1. You can initialize a pointer variable by applying the operator * to an existing variable of an appropriate type.

2. By using cin.get(), you can read ‘\n’ as a character.

3. Explicit constructor call returns a reference of an anonymous object of the corresponding class.

4. A dynamic variable should not be pointed by more than one pointer variables.

5. An object of a class A can be a private member of a class B.

6. Static member variables of a class can be modified by non-static member functions of that class.

7. A friend function of a class should not be overloaded to support automatic type conversion.

8. A function can return the name of an array by using the ‘return by reference’ mechanism.

9. You can’t have a private member variable of a certain name, when there already exists a public member variable of the same name.

10. Dangling pointers occur when the memory used by dynamic variables is not properly returned to the freestore (i.e., heap).





Short answer questions

11. Is there anything wrong with the following code? Explain your answer.
1
2
3
4
5
void func(int x)
{ int x; }
int func(int y)
{ int y;
return y;}


12. Is there anything wrong with the following code? If any, correct them.
1
2
3
4
5
6
7
8
int & func(int a[ ], int size)
{ int *t = new int[size];
return t; }[code]

13. Is there anything wrong with the following code? If any, correct them.
[code]char s[ ] = “boring exam”;
string xxx;
strcpy(s, xxx);


14. Write the declaration for the ‘addition operator +’ of Money class, which is implemented as a member function.

15. Write the declaration for the ‘addition operator +’ of Money class, which is implemented as a friend function.

16. Write the heading of a void type function that takes the following two dimensional array.
char page[30][100];

17. Write a code for creating a 3 by 5 dynamic array of integer type.

18. Write a code for implementing a function that swaps the values of two integer variables.

19. What is the difference of the two uses of ‘new’ operator in the following code?
1
2
3
int *n, *m;
n = new int (5);
m = new int [5];


20. Is there anything wrong with the following code? Explain your answer.
1
2
3
int *n;
n = new int (5);
n = new int [5];






Short programming questions

21.Write the implementation of the ‘operator <<’ of the following Money class. You should print ‘$’before the dollar amount. The dollar amount may be positive or negative.
1
2
3
4
5
6
7
class Money
{ public:
friend ostream& operator <<(ostream& outstream, const Money& amt);
private:
int dollars;
int cents;
}



22.Write a function that sorts the content of a static integer array.
sort(int a[ ], int size)

23.Write a C++ code that reads user’s text input and counts the number of words and lines.(Assume that words are separated by one or more white spaces)

24.Below source code is an example code of the word master that takes words and their meanings and displays them when requested by the user.

1) Find 2 logical errors from the source code (with the line number) and correct them.

2) Fill the 3 blanks with appropriate codes.

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;

#define MAX_WORD_SIZE 128
#define MAX_MEANING_SIZE 1024
//shows main menu and gets the user's choice
//Returns: the index of the menu that user selected.
int showMainMenu() {
...
// main menu
//1. Add Words
//2. Review
//0. Exit
...
}
// asks the user Yes or No with the given char string.
// Returns: true if user entered yes, false otherwise
bool askYN(const char* prompt)
{
...}
// adding a new word
// Returns: word count
int addWords([_____1_____])
{
char buffer[1024];
bool firstLine = true;
//repeat until the user quit
while(true) {
system("cls");
cout << "############################\n";
cout <<" WORD MASTER - Add Words\n";
cout << "############################\n\n";
// memory full!!!
if(wordsEntered == 3) {
// until you say YES
do {
cout << "Sorry, the memory is full.\n";
} while(!askYN("Would you go back to the main menu? (Y/N) "));
// return to main menu
return wordsEntered;
}
// get the new word
cout << "[Word] \n";
cin.getline(word[wordsEntered], MAX_WORD_SIZE);
// change the word to lower-case
[_________2__________]
// check if we have the same word already
bool hasDuplicate = false;
for(int i = 0; i < wordsEntered; ++i) {
if(strcmp(word[i], word[wordsEntered]) == 0) {
// we have a duplicate!
hasDuplicate = true;
break;
}
}
// if we have a duplicate, ask if the user wants to go back to main menu

// otherwise, start the loop again so the user can add other words
if(hasDuplicate) {
cout << "Error: We already have the word [" << word[wordsEntered] << "].\n";
if(askYN("Would you go back to the main menu? (Y/N)"))
return wordsEntered;
continue;
}
// get the word meaning
cout << "\n[Meaning] \n";
wordMeaning[wordsEntered][0] = 0;
firstLine = true;
while(true) {
cin.getline(buffer, MAX_MEANING_SIZE);
// if the line is "EOF", break
[______3_____]
if(!firstLine)
strcat(wordMeaning[wordsEntered], "\n");
else
firstLine = false;
strcat(wordMeaning[wordsEntered], buffer);
}
// finished entering a word
++wordsEntered;
if(!askYN("Add more? (Y/N) ")) break;
}
return wordsEntered;
}
// let the user review the words he/she entered
int review([______1_____])
{
int wordNo = 0;
// are we showing the menu for the first time?
bool isFirst = true;
char command[16];
while(true) {
isFirst = true;
// continue until the user makes a valid choice.
while(true) {
system("cls");
cout << "############################\n";
cout << " WORD MASTER - Review\n";
cout << "############################\n\n";
// show the list of words
for(int i = 0; i < wordsEntered; ++i)
cout << (i+1) << " - " << word[i] << "\n";
cout << "\n m - Back to main menu\n";
// show some error messages if we are not showing the menu for the first time
if(!isFirst)
cout << "\nError: Invalid input\n";
else
isFirst = false;
cout << "\ncommand >> ";
cin.getline(command, 3);
// return to main menu
if(command[0] == 'm') return wordsEntered;
// the user entered a valid number?
if(isdigit(command[0]) && 1 <= atoi(command) && atoi(command) <= wordsEntered) break;
}
// show the word the user selected.
wordNo = atoi(command) - 1;
cout << "\n[Word]\n";
cout << word[wordNo] << "\n";
cout << "\n[Meaning]\n";
cout << wordMeaning[wordNo] << "\n";
cout << "\n(Hit 'm' to go back to the list)\n";
// wait until the user hits m
while(getchar() == 'm');
getchar(); //just to trash the newline char
}
return wordsEntered;
}
int main()
{
bool running = true;
// the word counter: the number of words, user added so far
int wordsEntered = 0;
// the words: the words themselves
char word[3][MAX_WORD_SIZE];
// the meaning of the words
char wordMeaning[3][MAX_MEANING_SIZE];
// Continue until the user ask you to stop
while(running) {
int choice = showMainMenu();
switch(choice) {
case 0: // user asked to quit: set running to false to quit this while loop
running = false;
case 1: // add words
wordsEntered = addWords(word, wordMeaning, wordsEntered);
break;
case 2: // review
// do some review
wordsEntered = review(word, wordMeaning, wordsEntered);
break;
}
}
return 0;
}
Last edited on
I know nothing about it


Before you edited your post, the link you gave mentioned it was the mid-term. Which means you have been in this C++ course for at least half a semester.

If you don't know anything about how to answer the above questions.... then what exactly were you doing in class all this time?



Anyway, this isn't a homework service. Nobody is going to answer these questions for you.

Obligatory link: http://cplusplus.com/forum/articles/1295/


EDIT: to clarify, we don't mind helping you if you're making a clear effort and want to learn this stuff. But the way it sounds is like you don't really care about learning and just want someone to do your work for you.

But prove me wrong. Try to do the above yourself and show us what you came up with. Then maybe someone will point out your mistakes and show you how to correct them.
Last edited on
Before you guys get to this could you do all my coding work for the day first. It's not very interesting and I'd rather go fishing. PLLzzzz... I'll really appreciate it!

Sorry, I couldn't resist. The homework questions are bad enough but now... Do my TEST for me!

Unbelievable.
NO!!!

As Disch said, we are not homework or test service. EDIT: We're the world's most complex compiler. Sort of. :)

@cnoeval: If you'd rather go fishing, I understand. I'd rather be coding on a stable absolutely-perfect GNU/Linux system than Mac OS X.
Here's a neat idea... how about you do the problem solving WHILE you go fishing?

@waxth: http://cplusplus.com/doc/tutorial/

-Albatross
Last edited on
Here's a neat idea... how about you do the problem solving WHILE you go fishing?


Hey, are you trying to "bait" me??? If I do that all I'll catch are exceptions!
Oh man. That was a pretty aweful joke, cnoeval

XD
:P

But seriously, why not? Correct me if I'm mistaken but fishing is mostly setting up a trap of sorts and waiting, right?

-Albatross
Oh man. That was a pretty aweful joke, cnoeval


What do you expect from someone with the username C - No - Eval???

@Albatross: Very true, we use bugs and worms to fish, and in programming we fish for bugs and worms.
waxth

like the guys said give it a try first okay , i am too a student and i tell you it is much fun to figure out the solution by yourself rather than making the others do it for you and if you couldn't solvei t by yourself then w'll give you a hand, but your sheet is not that hard even if you know a litte
Topic archived. No new replies allowed.