View Full Version : Some Help With C++ Needed.
I needed a quick credit for my degree this semester, so I decided to take a Comp Sci class since I had heard it was easy. So far it isn't too bad, but I'm stuck on this piece of code am unsure of where to go.
create a program that asks for the size of the multiplication table. Your customized border should be resizable based upon the size of the multiplication table.
I've made the program ask for the size of the table, and have a border of * at the top and bottom, but I'm not sure how to make it resizable or how to have * go down on either the left or right side to create the impression of a frame.
Any and all help is much appreciated.
MadChedar0
09-27-2006, 08:39 PM
I think they mean like the numbers for the dimensions of the table aren't hard coded into your program right?
If that's the case just leave the program in a looping state that keeps taking input until you hit some designated termination number (like -1):
int newDimension = 0;
while( ( cin >> newDimension ) != '-1' )
{
/* Keep rebuilding the table with your stars or whatever */
/* Then ask for the dimension again or something */
}
I'd have to see the assignment to be sure that's what they want... but yeah maybe that helps.
I think they mean like the numbers for the dimensions of the table aren't hard coded into your program right?
If that's the case just leave the program in a looping state that keeps taking input until you hit some designated termination number (like -1):
int newDimension = 0;
while( ( cin >> newDimension ) != '-1' )
{
/* Keep rebuilding the table with your stars or whatever */
/* Then ask for the dimension again or something */
}
I'd have to see the assignment to be sure that's what they want... but yeah maybe that helps.
Well, I could show you what I have built, but I'm not sure how to copy and paste of PuTTY. Yes I know, my school is extremely cheap.
Actually, here it is:
int main ()
{
int x, y, NumColumn, NumRow;
printf ("What is the height you would like your multiplication table to be?""\n");
scanf ("%d", &NumColumn);
printf ("What is the width you would like your multiplication table to be?""\n");
scanf ("%d", &NumRow);
//Prints out first border row of *
for(x=1;x<=NumRow*5+3; x++)
{
printf("*");
}
printf("\n");
//Prints out multiplication table
printf(" ");
for(y=1;y<=NumColumn; y++)
{
printf("%5d", y);
}
printf("\n");
for(x=1;x<=NumRow; x++)
{
printf(" ");
printf("\n");
printf("%3d", x);
for(y=1;y<=NumColumn; y++)
printf("%5d", y*x);
printf("\n");
}
//Prints out final border * row
for(x=1;x<=NumRow*5+3; x++)
{
printf(" ");
printf("\n");
printf("%3d", x);
for(y=1;y<=NumColumn; y++)
printf("%5d", y*x);
printf("\n");
}
//Prints out final border * row
for(x=1;x<=NumRow*5+3; x++)
{
printf("*");
}
printf("\n");
}
////////////////////////
I know in the second to last part where it prints out the table it is supposed to be a for loop within a for loop, but when I place the {} around that second for loop it screws it all up and I'm not sure where/what to fix (if any).
MadChedar0
09-27-2006, 08:49 PM
Putty opens up as a regular screen terminal, you should be able to click something like "Edit-->Select All" which will select everything in the terminal up to a point.... but then you can select one contiguous block of text with your cursor. Once that's selected you can Ctrl-C and copy the text you selected.
Then paste wherever.
Hopefully that made sense.
Edit: So what screws up exactly?
Putty opens up as a regular screen terminal, you should be able to click something like "Edit-->Select All" which will select everything in the terminal up to a point.... but then you can select one contiguous block of text with your cursor. Once that's selected you can Ctrl-C and copy the text you selected.
Then paste wherever.
Hopefully that made sense.
shoot your right. I could have just copied to the clipboard. Totally overlooked that part. Still, putty stinks compared to working with Microsoft VC.
Edit: So what screws up exactly?
I need to create a border of my choosing that goes around the entire table and stretch or shorten depending on the inputs given.
I can get a top frame, but then i can't get it to expand or decrease and I can't get it to go all the way around.
MadChedar0
09-27-2006, 08:56 PM
So while you're building each row, print one border character, then tab, print the board, then print the other border character.
So like
//Prints out multiplication table
printf(" "); // place your character in here too plus another space
for(y=1;y<=NumColumn; y++)
{
printf("%5d", y);
}
// place another border character here too
Just keep experimenting.. you'll get it
To expand it, just accept new values for the table dimensions and rebuild it. And I think you'll have it.
So while you're building each row, print one border character, then tab, print the board, then print the other border character.
So like
//Prints out multiplication table
printf(" "); // place your character in here too plus another space
for(y=1;y<=NumColumn; y++)
{
printf("%5d", y);
}
// place another border character here too
Just keep experimenting.. you'll get it
To expand it, just accept new values for the table dimensions and rebuild it. And I think you'll have it.
I'm not exactly sure what you mean. I placed the printf("*") and it gave me one * under the original row I had done, but it would not expand with different dimensions. Same with my original two rows. I don't get how to make them expand or contract with different table dimensions. For instance upto 12 dimensions on either side the work and fit, but above that they don't.
the_gloaming
09-27-2006, 09:15 PM
It's been a while since I used C++, but ...
In this code segment:
for(x=1;x<=NumRow*5+3; x++)
{
printf(" ");
printf("\n");
printf("*"); // This is the start of a new row, so put an asterik here to make your leftside border.
printf("%3d", x);
for(y=1;y<=NumColumn; y++)
{printf("%5d", y*x);}
printf("*"); // This is the end of each row, so add an asterik here to close the right side.
printf("\n");
}
SteveMcQ
09-27-2006, 09:27 PM
Don't forget your ';', those things drove me nuts after pages and pages worth of codes and getting compiling errors. Damn you ';'!!!!!!!!!!!!1
camoor
09-27-2006, 09:34 PM
Don't forget your ';', those things drove me nuts after pages and pages worth of codes and getting compiling errors. Damn you ';'!!!!!!!!!!!!1
LoL - computer lab fun, replace multiple random ; with : when your buddy takes a break.
Quillion
09-27-2006, 09:42 PM
It's been a while since I used C++, but ...
In this code segment:
for(x=1;x<=NumRow*5+3; x++)
{
printf(" ");
printf("\n");
printf("*"); // This is the start of a new row, so put an asterik here to make your leftside border.
printf("%3d", x);
for(y=1;y<=NumColumn; y++)
{printf("%5d", y*x);}
printf("*"); // This is the end of each row, so add an asterik here to close the right side.
printf("\n");
}
This should work, just from glancing at it. I'd be a little concerned at the order of operations in "x<=NumRow*5+3", and I'd change the names NumRow and NumColumn for reading clarity, but those are style issues.
I haven't written a C++ Command Line/Console program in years.
Oh, if you're allowed to use different compilers, you can use Microsoft Visual C++ for free.
http://msdn.microsoft.com/vstudio/express/
It doesn't come with all the objects, or the MSDN library, but you can't argue with the price. If you plan on pursuing programming in a professional capacity, you really should learn .NET.
the_gloaming
09-27-2006, 09:42 PM
As for putting together your top and bottom rows, it depends on a few things. The number of asteriks should be something like ...
//begin pseudocode
MaxDigits = Maximum number of digits used in the column (for example, on a 10 x 10 table, with the highest factor being 10, the largest number of digits would be 3. i.e., 10 x 10 = 100, 100 has three digits).
NumSpaces = The number of spaces you've allocated between each column.
NumAsteriks = NumColumns * ( NumSpaces + MaxDigits )
for(int i = 0; i < NumAsteriks; i++)
printf("*");
Use NumAsteriks as your loop control variable and you should be relatively close (you'll need to adjust for a little bit more because this will still be short by a few asteriks, based on your left and right side borders).
I think that might work, but I don't have a compiler handy so I can't be 100% on it.
As for putting together your top and bottom rows, it depends on a few things. The number of asteriks should be something like ...
//begin pseudocode
MaxDigits = Maximum number of digits used in the column (for example, on a 10 x 10 table, with the highest factor being 10, the largest number of digits would be 3. i.e., 10 x 10 = 100, 100 has three digits).
NumSpaces = The number of spaces you've allocated between each column.
NumAsteriks = NumColumns * ( NumSpaces + MaxDigits )
for(int i = 0; i < NumAsteriks; i++)
printf("*");
Use NumAsteriks as your loop control variable and you should be relatively close (you'll need to adjust for a little bit more because this will still be short by a few asteriks, based on your left and right side borders).
I think that might work, but I don't have a compiler handy so I can't be 100% on it.
I tried that just now and it gave me hardly any * at the top columns. Thanks for the help though.
This should work, just from glancing at it. I'd be a little concerned at the order of operations in "x<=NumRow*5+3", and I'd change the names NumRow and NumColumn for reading clarity, but those are style issues.
I haven't written a C++ Command Line/Console program in years.
Oh, if you're allowed to use different compilers, you can use Microsoft Visual C++ for free.
http://msdn.microsoft.com/vstudio/express/
It doesn't come with all the objects, or the MSDN library, but you can't argue with the price. If you plan on pursuing programming in a professional capacity, you really should learn .NET.
It works, but it only gives me an * on every line of the table, but I need a * for each row including the ones with just white space on them.
the_gloaming
09-29-2006, 04:01 AM
I quoted my old segment, but just add in the other "*" to get it to appear on every line.
It's been a while since I used C++, but ...
In this code segment:
for(x=1;x<=NumRow*5+3; x++)
{
printf(" ");
printf("*"); // New addition -- consider dropping the above " " and replace it with this line.
printf("\n");
printf("*"); // This is the start of a new row, so put an asterik here to make your leftside border.
printf("%3d", x);
for(y=1;y<=NumColumn; y++)
{printf("%5d", y*x);}
printf("*"); // This is the end of each row, so add an asterik here to close the right side.
printf("\n");
printf("*"); // New addition
}
As for making top and bottom borders, you just need a loop that looks something like this:
for(i=0; i<=VARIABLE; i++)
{ printf("*"); }
Make sure "VARIABLE" is an integer value that contains the number of asteriks you want. You may have to calculate this value on your own based on your input.
WinnieThePujols
09-29-2006, 02:11 PM
LoL - computer lab fun, replace multiple random ; with : when your buddy takes a break.
At first I found that really funny, but it's kind of spoiled by the fact that when you go to compile your editor is going to say, "Syntax error: line X" or whatever. Now if it didn't give you a line number... :)
SneakyPenguin
09-29-2006, 07:25 PM
A better way to mess with people is to change al of their "=="s to just "="s, and it will usually still compile, just it'll explode.