Click to See Complete Forum and Search --> : Bonus Question
aph3x
12-08-2000, 03:08 AM
well, i had my first semester c++ final exam today... we had two bonus questions, one of which i couldnt figure out. so here i am asking the experts http://www.linuxnewbie.org/ubb/biggrin.gif
the question was something like this:
form a matrix according to user input, ie: if the user enters 5, form a 5 by 5 matrix. the form of the matrix (for a 5x5) would look like this:
?0000
*?000
**?00
***?0
****?
you are only allowed to use three loops... any ideas?
Stuka
12-08-2000, 03:28 AM
1. Get Input (call it integer x)
2. Create array: char arr[x][x]
3. Nested loops:
a: Outer loop - x iterations
b: Inner loop - also x iterations
c: if statement: if (inner loop var) < (outer loop var) arr[i.l.v.][o.l.v.] = '*'
else if (i.l.v.) < (o.l.v.) arr[i][o] = '0'
else arr[i][o] = '?'
Whaddya think?
Strike
12-08-2000, 05:05 AM
#include <iostream>
using namespace std;
int main()
{
int dim=0;
cout << "Enter dimension: ";
cin >> dim;
char mat[dim][dim];
for (int row=0; row < dim; ++row) {
for (int col=0; col < dim; ++col) {
if (col < row) {
mat[row][col] = '*';
cout << "*";
} else if (col == row) {
mat[row][col] = '?';
cout << "?";
} else {
mat[row][col] = '0';
cout << "0";
}
}
cout << endl;
}
return 0;
}
Strike
12-08-2000, 05:06 AM
That assigns to the array as well as printing out what you are assigning.
Stuka
12-08-2000, 01:23 PM
Strike, you showoff! http://www.linuxnewbie.org/ubb/smile.gif Anyway, how's he gonna learn if he don't implement it himself? Oh, well, just make me look bad. http://www.linuxnewbie.org/ubb/tongue.gif
Strike
12-08-2000, 05:02 PM
It doesn't really make you look bad, I mean it's pretty much exactly the pseudocode you laid out. I just put the includes, the return statement, and other of that required garbage in. Besides, he said the test was over http://www.linuxnewbie.org/ubb/smile.gif
aph3x
12-10-2000, 12:17 AM
in a Darth Vader voice, "Impressive..."
http://www.linuxnewbie.org/ubb/biggrin.gif
tnordloh
12-10-2000, 03:51 PM
I had a problem that almost exactly followed this logic on one of my assignments. That means the teachers like this one. Future cs students be warned to understand this (it's not that tough)
http://www.comsc.ucok.edu/~vollert/CAssgts/cassgt3.html
------------------
Laziness; Writing a 1000 line script to save 10 keystrokes. -AEleen Frisch
f'lar
12-10-2000, 09:28 PM
Actually if I remember right you can't use a variable to allocate an array like that. It would have to be more like this:
int main()
{
int size;
cin >> size;
int * array;
array = new int[size][size];
//... all that other stuff you did ...
delete [] array;
return (0);
}
------------------
If life were fair, we'd all be in Hell already.
[This message has been edited by f'lar (edited 10 December 2000).]
[This message has been edited by f'lar (edited 10 December 2000).]
f'lar
12-10-2000, 09:29 PM
One more thing: Since I'm just about to finish linear algebra(with any luck my last math class ever!), I'll mention that it is much more desirable to have the matrix look like this:
?****
0?***
00?**
000?*
0000?
A_Lawn_GNOME
12-10-2000, 09:40 PM
Heh, I can't wait to see people on the CS AP exam. The essay question was:
Use a stack to convert another stack from infix to prefix notation using tokens of form blah blah blah.
Thank God they explained what *fix notation was.
When I finally learned Lisp in college, it made sense.
Btw, for all the non-CS majors, prefix is like "+ 3 4" instead of "3 + 4". It almost made sense except Lisp drove me nuts with the lack of any kind of loop.
Stuka
12-11-2000, 01:32 AM
So true Strike - I forgot to put the <sarcasm> tag on my reply. Actually this was fairly easy for me, since I coded an app that drew a "quilt" pattern (it was in C, since that was the only graphics lib we had easy access to in Windows), for my first C++ class. The quilt was laid out like so:
12345
23451
34521
45123
51234
Where each number was a predefined graphics block, 3 of which had a pattern given by the instructor. My main() was <10 lines...I was proud of that!
Strike
12-11-2000, 01:50 AM
Originally posted by f'lar:
Actually if I remember right you can't use a variable to allocate an array like that. It would have to be more like this:
int main()
{
int size;
cin >> size;
int * array;
array = new int[size][size];
//... all that other stuff you did ...
delete [] array;
return (0);
}
Yeah, you can do what I did. It works - try it.
f'lar
12-11-2000, 05:10 AM
My bad. I thought you had to have a constant inside the brackets.
A_Lawn_Gnome: That exact problem was a programming assignment for our comp 245 class (basically, the 2nd computer class you take here). I was one of the easier programs I wrote for that class. The only thing was that the stack class had to use a dynamically allocated linked list.
------------------
If life were fair, we'd all be in Hell already.