-
October 2nd, 2016, 06:00 PM
#1
Array of 100 elements ranging from 1 to 100
Hello!
I'm trying to create an array of 100 elements, ranging from 1 to 100.
Code:
int main()
{
int arr[100];
// For setting values to elements
for (int i = 0; i < 101; i++)
{
arr[i] = i;
}
// For getting values from elements
for (int i = 0; i < 101; i++)
{
cout << arr[i] << endl;
}
return 0;
}
The problem with this code is that in element 0 I have 0, in element 1 I have 1, in element 2 I have 2, and so on.
I need number 1 in element 0, number 2 in element 1, number 3 in element 2, and so on. What am I missing?
-
October 2nd, 2016, 07:40 PM
#2
Re: Array of 100 elements ranging from 1 to 100
I think I found the problem. Here is the solution:
Code:
int main()
{
int arr[100];
// For setting values to elements
for (int i = 0; i < 100; i++)
{
arr[i] = i + 1;
}
// For getting values from elements
for (int i = 0; i < 100; i++)
{
cout << arr[i] << endl;
}
return 0;
}
Last edited by 2kaud; October 3rd, 2016 at 02:24 AM.
-
October 3rd, 2016, 02:30 AM
#3
Re: Array of 100 elements ranging from 1 to 100
Yep. Just a comment. When dealing with a fixed size array, IMO it is better to have a const variable to hold the number of elements in the array. That way the loops etc get the correct terminating condition and if the array size changes, only one value in the program is changed. Note that the variable needs to be const as the c++ compiler needs to know the value at compile time. Consider
Code:
int main()
{
const int arrsize = 100;
int arr[arrsize];
// For setting values to elements
for (int i = 0; i < arrsize; i++)
arr[i] = i + 1;
// For getting values from elements
for (int i = 0; i < arrsize; i++)
cout << arr[i] << endl;
return 0;
}
Also note that if there is only one statement following the for() then braces are not required (same for if etc).
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)
-
October 3rd, 2016, 02:48 AM
#4
Re: Array of 100 elements ranging from 1 to 100
Originally Posted by 2kaud
Also note that if there is only one statement following the for() then braces are not required (same for if etc).
However, you may wish to consistently put the braces even when they are not required so as to avoid having them accidentally left out should the code be changed such that braces become required, or when code is introduced with poor indentation such that it appears to be part of the loop body when it isn't.
Tags for this Thread
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
|