|
-
May 5th, 2009, 04:06 PM
#16
Re: arrays
the code you have there won't even compile, once you fix this you might find that you've already done what you wanted. The setw hangs up the compiler, it's not a part of the iostream library in that form, though I don't know how to use it. I can help your recoding problem somewhat.
notice how this: void display_array (......); is different than these:
int display_array(.....);
float display_array(.....);
double display_array(.....);
char display_array(.....);
you treat void differently than the others inside the {} brackets. Typing void in front of the identifier means that it dose NOT return a value. This also means you do not need to place the "return;" statement at the end of your function. Otherwise you will need to return a char, int, float or double.
Secondly cout can be uses multiple times after it has been called. You've done this allready
here: cout << "\nOffset: " << setw(2) << i;
so telling it to type a little more by placeing the spaces between <<'s.
-
July 11th, 2009, 08:49 AM
#17
Re: arrays
Well, I've not run that code, but it seems to run off the end of the screen as it is. the computer would automaticly place words on the next line down once this happened or it might just type over the last ones. you need to insert an <<endl at the end of so many cout statements to properly get it to go down a line. To do this you'll need to have an if statment in the for loop that says someing like this:
if (i% NumberOfEntriesWantedOn_A_Line==0 && i!=0)
{
cout<<endl;
}
that way it will only activate the number of entries wanted. The % sign means that if you devide
10 by 8 (written like this in code: 10\8 ) the computer can give you two answers the nomal one is 1 because 10 is divided into 8 one time. the second answer the computer could give you is the remainder of the numbers once divided. so 10-8 is two, therefore, the "modulus" of 10/8 is 2. the % returns the two and is an agruement. Note that the if statement I wrote has an agruement asking if the remainder is zero. and if i itself is not a zero (because 0/anyghing is 0.)
As to the "Best" way to get your task done, well, there are lots of ways to do most everything in programing. Thats part of why I like it. You get to design it how you want. Hope that helps.
-
April 23rd, 2014, 05:56 PM
#18
Re: arrays
 Originally Posted by ehoi
Well, I've not run that code, but it seems to run off the end of the screen as it is. the computer would automaticly place words on the next line down once this happened or it might just type over the last ones. you need to insert an <<endl at the end of so many cout statements to properly get it to go down a line. To do this you'll need to have an if statment in the for loop that says someing like this:
if (i% NumberOfEntriesWantedOn_A_Line==0 && i!=0)
{
cout<<endl;
}
that way it will only activate the number of entries wanted. The % sign means that if you devide
10 by 8 (written like this in code: 10\8 ) the computer can give you two answers the nomal one is 1 because 10 is divided into 8 one time. the second answer the computer could give you is the remainder of the numbers once divided. so 10-8 is two, therefore, the "modulus" of 10/8 is 2. the % returns the two and is an agruement. Note that the if statement I wrote has an agruement asking if the remainder is zero. and if i itself is not a zero (because 0/anyghing is 0.)
As to the "Best" way to get your task done, well, there are lots of ways to do most everything in programing. Thats part of why I like it. You get to design it how you want. Hope that helps.
// Headers and Other Technical Items
#include <iostream>
using namespace std;
// Function Prototypes
void pause(void);
// Variables
int iq[8] = {80,90,100,110,120,130,140,150};
double money[6] = {1.21,1.31,1.41,1.51,1.61,1.75};
int x; // Used as a flag to control for loops
//******************************************************
// main
//******************************************************
int main(void)
{
cout << "\nThe oiginal IQ's:" << endl;
for(x = 0; x < 5; x++)
{
cout << iq[x] << endl;
}
cout << "\n\nThe second person became smarter, what is his new IQ -->: ";
cin >> iq[1];
cout << "\n\nThe revised IQ:" << endl;
for(x = 0; x < 5; x++)
{
cout << iq[x] << endl;
}
pause();
cout << "\n\nThe money for 6 members:" << endl;
for(x = 0; x < sizeof money / sizeof money[0]; x++)
{
cout << money[x] << endl;
}
pause();
return 0;
}
//******************************************************
// pause
//******************************************************
void pause(void)
{
cout << "\n\n";
system("PAUSE");
cout << "\n\n";
return;
}
//******************************************************
// End of Program
//******************************************************
-
April 24th, 2014, 05:32 AM
#19
Re: arrays
Whn you post code, please use code tags. Go advanced, select the code and click '#'.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 24th, 2014, 06:40 AM
#20
Re: arrays
 Originally Posted by Zaccheus@Work
Not to mention holding financial information in floats!
would be just very slightly less worse had it been doubles 
some people seem to be smart enough to see floats won't be good enough, but run into the pitfall thinking that doubles don't have the same inate problems as floats do.
-
April 24th, 2014, 07:17 AM
#21
Re: arrays
 Originally Posted by 2kaud
Whn you post code, please use code tags. Go advanced, select the code and click '#'.
Probably even more important, when you bump threads, try to make sure they're not five years old.
-
April 24th, 2014, 09:07 AM
#22
Re: arrays
Will do, thanks for the advice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|