I was trying to write a program to take an input and output the reverse of that input; here was my code:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num[n];
for (int i=1; i<=n; i++)
{
cin >> num[i];
}
for (int i=n; i>=0; i--)
{
cout << num[i] << " ";
}
return 0;
}
I realized that in the second for loop, i can equal 0 and then i equals -1. However, the outputs don't really make sense. For example, an input of
6
8 1 2 6 3 9
results in
9 3 6 2 1 8 8 8
and a lot of the time it's just the array reversed but with an 8 added onto the end, but sometimes there are these types of numbers:
9
1 0 2 8 1 4 2 9 8
8 9 2 4 1 8 2 0 1 1703936
Where do these ending numbers come from? Because I don't understand what's going on, I can't generalize what the problem is, but if you have an easily-accessible IDE nearby and will copy and paste my code (assuming it's not a well-known problem and I'm making everyone laugh at my stupidity), could anyone tell me why there are these numbers added onto the end?
for (int i=1; i<=n; i++)
In C++, array indexes are 0-indexed. This means if your array is size n, the valid indexes are 0, 1, 2, ..., n-1.
Your loop will loop over 1, 2, 3, ..., n. Do you see the issue? You never write to the 0'th index, and you write one past the last index (which is not allowed).
Then when you go to print:
for (int i=n; i>=0; i--)
You print n, n-1, n-2, ..., 0. You're printing the n'th element, which is again, not allowed.
Instead, your loops should look like:
for (int i=0; i<n; i++)
{
cin >> num[i];
}
for (int i = 0; i < n; i++)
{
cout << num[n - i - 1] << " ";
}
Your second loop begins with i equal to n ... but that's not a valid array index, because an array containing n elements has indexes [0..n-1]! Therefore, you are seeing "memory garbage."
So, your second loop should be:
for (int i=n-1; i>=0; i--)
You seem to think that arrays are indexed from 1 which is your main issue.
for (int i=1; i<=n; i++)
The last element of an array if length-1, so if you had 3 elements the last index is 2, not 3; however, the loop above starts at 1 (the second element) and then accesses an index out of bounds, which is undefined behaviour.
But really you don't need to use indices at all, just for (auto& i : n) will iterate properly over your container. If you want to loop using indices, you need
for (int i = 0; i < n; i++) // forwards
for (int i = n-1; i >= 0; i--) //backwards
It's worth noting that variable length arrays (that is, arrays whose lengths are not known at compile time) are not standard C++, but are an extension of GCC. It would be worth ditching that behaviour now, and using vector instead:
int length = 0;
std::cin >> length;
std::vector<int> n(length);
Related
I want my array to maintain a 1 based indexing even after the sort() is completed. My code below gives me a garbage value at the last index when i = n because the loop should have been
for(int i = 0; i < n; i++) while printing the code after the sort is completed. Is there a way to use std::sort() and still maintain a 1 based indexing?
int n;
cin >> n; // 6
int a[n+1];
for(int i = 1; i <= n; i++)
cin >> a[i]; // INPUT ARRAY: 6 4 2 7 2 7
sort(a, a+n+1);
for(int i = 1; i <= n; i++)
cout << a[i] << " "; // OUTPUT AFTER SORTING: 2 4 6 7 7 6421920
Well yes, only sort starting from index 1.
std::sort(a+1,a+n+1);
Yes, there is a way. Just omit the dummy element when you call sort:
sort(a+1, a+n+1);
I was given the integers 15, 16, 17 ,18 ,19 and 20.
I am supposed to put only the numbers divisible by 4 into a vector and then display the values in the vector.
I know how to do the problem using arrays but I'm guessing I don't know how to properly use pushback or vectors.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> arrmain; int i,j;
for (int i = 15; i <=20 ; i++)
{
//checking which numbers are divisible by 4
if (i%4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
//output the elements in the vector
for(j=0; j<=arrmain.size(); j++)
{
cout <<arrmain[i]<< " "<<endl;
}
}
}
return 0;
}
wanted output: Numbers divisible by 4: 16, 20
As already mentioned in the comments, you have a couple of problems in your code.
All which will bite you in the end when writing more code.
A lot of them can be told to you by compiler-tools. For example by using -Weverything in clang.
To pick out the most important ones:
source.cpp:8:10: warning: declaration shadows a local variable [-Wshadow]
for (int i = 15; i <=20 ; i++)
and
source.cpp:6:26: warning: unused variable 'i' [-Wunused-variable]
vector arrmain; int i,j;
Beside those, you have a logical issue in your code:
for values to check
if value is ok
print all known correct values
This will result in: 16, 16, 20 when ran.
Instead, you want to change the scope of the printing so it doesn't print on every match.
Finally, the bug you are seeing:
for(j=0; j<=arrmain.size(); j++)
{
cout <<arrmain[i]<< " "<<endl;
}
This bug is the result of poor naming, let me rename so you see the problem:
for(innercounter=0; innercounter<=arrmain.size(); innercounter++)
{
cout <<arrmain[outercounter]<< " "<<endl;
}
Now, it should be clear that you are using the wrong variable to index the vector. This will be indexes 16 and 20, in a vector with max size of 2. As these indexes are out-of-bounds for the vector, you have undefined behavior. When using the right index, the <= also causes you to go 1 index out of the bounds of the vector use < instead.
Besides using better names for your variables, I would recommend using the range based for. This is available since C++11.
for (int value : arrmain)
{
cout << value << " "<<endl;
}
The main issues in your code are that you are (1) using the wrong variable to index your vector when printing its values, i.e. you use cout <<arrmain[i] instead of cout <<arrmain[j]; and (2) that you exceed array bounds when iterating up to j <= arrmain.size() (instead of j < arrmain.size(). Note that arrmain[arrmain.size()] exceeds the vector's bounds by one because vector indices are 0-based; an vector of size 5, for example, has valid indices ranging from 0..4, and 5 is out of bounds.
A minor issue is that you print the array's contents again and again while filling it up. You probably want to print it once after the first loop, not again and again within it.
int main()
{
vector<int> arrmain;
for (int i = 15; i <=20 ; i++)
{
//checking which numbers are divisible by 4
if (i%4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
}
}
//output the elements in the vector
for(int j=0; j<arrmain.size(); j++)
{
cout <<arrmain[j]<< " "<<endl;
}
return 0;
}
Concerning the range-based for loop mentioned in the comment, note that you can iterate over the elements of a vector using the following abbreviate syntax:
// could also be written as range-based for loop:
for(auto val : arrmain) {
cout << val << " "<<endl;
}
This syntax is called a range-based for loop and is described, for example, here at cppreference.com.
After running your code, I found two bugs which are fixed in code below.
vector<int> arrmain; int i, j;
for (int i = 15; i <= 20; i++)
{
//checking which numbers are divisible by 4
if (i % 4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
//output the elements in the vector
for (j = 0; j < arrmain.size(); j++) // should be < instead of <=
{
cout << arrmain[j] << " " << endl; // j instead of i
}
}
}
This code will output: 16 16 20, as you are printing elements of vector after each insert operation. You can take second loop outside to avoid doing repeated operations.
Basically, vectors are used in case of handling dynamic size change. So you can use push_back() if you want to increase the size of the vector dynamically or you can use []operator if size is already predefined.
Write a program that uses an array of size eight (8) and then prompts
the user to input eight integers for the array. The program should do
the following:
Print the numbers in the output given in the sample Input/Output.
The user is prompted to enter an integer value k, the program will then shift the array elements k positions to the right, while the last
K elements are moved to the beginning of the array. For example: if
we have an array [1 2 3 4 5 6 7 8], shifting 2 positions to the right
should give the array [7 8 1 2 3 4 5 6].
I did this question but I got the output (if assumed number of shift = 3)
[1 0 3 1 2 3 4 5] which is wrong, the output should be [6 7 8 1 2 3 4 5]
This is my code, what is the mistake?
#include<iostream>
using namespace std;
int main()
{
int arrays[8];
int k;
cout<<"Enter Values for a Arrays of size 8"<<endl;
for(int i=0; i<8; i++)
{
cin>>arrays[i];
}
cout<<"You Entered Numbers are: [ ";
for(int i=0; i<8; i++)
{
cout<<arrays[i]<<" ";
}
cout<<" ]\n";
cout<<"Enter the Number of Shift: ";
cin>>k;
for(int i=8; i>0; i--)
{
arrays[i]=arrays[i-k];
}
//The Output
for(int i=0; i<8; i++)
{
cout<<arrays[i]<<" ";
}
return 0;
}
The error is in
for(int i=8; i>0; i--)
{
arrays[i]=arrays[i-k];
}
First of all when i<3 you get a negative index. You can solve this using the % operator which returns the reminder.
Secondly you are overwriting the values that still must be copied. For example suppose the shifting is 3: at the first iteration you copy the fifth element inside the eighth. But then you should also copy the eighth inside the third.
One simple solution is to declare a new array and do the following:
int shifted[8];
...
for (int i=0; i<8; i++)
{
shifted[i] = arrays[(i-k)%8]
}
Consider also using C++ vectors.
In an array, the index of the first element is zero. So an array declared as:
int a[5];
should have a[4] as the last element. However, when I execute the following code:
int n;
cin>>n;
int a[n];
for(int i = 0; i++<n; cin>>a[i]);
for(int i = 0; i++<n; cout<<i<<" "<<a[i]<<endl);
And give the input as:
5 1 2 3 4 5
The output is:
1 1
2 2
3 3
4 4
5 5
That implies that a[5] = 5. I was wondering as to how that is possible. Shouldn't it display some garbage value instead?
change i++ < n to i < n and cin>>a[i] to cin>>a[i++]
Logical. You are doing it wrong. Consider corner where n = 5:
Your i is 4. You do check if i++ < 5 is true and after check, i increases.
I is now 5 and this is out of bounds.
Your loop actually runs from 1 to 5 instead of 0 to 4.
Rewrite codes to:
for (int i = 0; i < n; cin >> a[i], i++);
for (int i = 0; i < n; cout << i << " " << a[i] << endl, i++);
Shouldn't it display some garbage value instead?
Teaching that an unitialized variable has a garbage value or that accessing an array will yield garbage values is really a disservice.
Accessing an uninitialized variable or an array out of bounds in C++ is Undefined Behavior. This means that the program can behave in any way. It can crash, it can appear to function, it can any behavior, it can appear to have garbage values in unitialized variables.
Alternatively you could just replace
int i=0 by int i=-1 and,
i++ by ++i
Now the loop will run and enter the values from a[0] to a[4] as expected.
The code could be rewritten as:
for(int i=-1;++i<n;cin>>a[i]);
for(int i=-1;++i<n;cout<<i<<" "<<a[i]<<endl);
Ok so last night I taught my sister some basic code how to read input, put it in array and print it to the screen. Here is the code :
int main() {
int N; // size of array
cin >> N;
int arr[N];
for(int i = 1; i<= N; i++) {
cin >> arr[i];
}
for(int i = 0; i<N; i++) {
cout << arr[i] << endl;
}
return 0;
}
And here is the result :
input :
4
2 3 4 1
output :
8
2
3
4
I don't have any clues why this can happen, I tried to check the code but it seems simple and already correct for me.
First of all C++ does not support variable length arrays and if an array is declared as having N elements then the valid range of indices is [0, N-1].
You could either dynamically allocate an array of the required size yourself or use standard container std::vector<int>.
Using std::vector<int> you could for example write
#include <iostream>
#include <vector>
int main()
{
unsigned int n = 0;
std::cout << "Enter number of elements: ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
for ( int &x : v ) std::cin >> x;
std::cout << "\nThe array is ";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The program output might look like
Enter number of elements: 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array is 0 1 2 3 4 5 6 7 8 9
The first thing you have to look after is that dasblinkenlight has already mentioned in his answer, variable-length arrays are a language extension.
Then you should think that arrays are zero index based. What does this means? Let's consider an array of size 4 that is denoted as arr. Then it's elements can be accessed as arr[0], arr[1], arr[2], and arr[3]. So what happens when you run your first loop?
for(int i = 1; i<= N; i++) {
cin >> arr[i];
}
You set the a value for arr[1], arr[2] to arr[N]. But the arr[N] points to a place in memory that is not associated with the array, since the array's size is N and all arrays are zero based. The last element of the array lives in here arr[n-1].
Unfortunately in C++ there isn't any check for the length of an array and you are allowed to do things like this. For instance, if you had tried this in C# or in Java then you would have got an Index Out Of Range Exception.
If you change your code to the following, then you will get the expected output:
for(int i = 0; i<N; i++) {
cin >> arr[i];
}
for(int i = 0; i<N; i++) {
cout << arr[i] << endl;
}
Your code is not valid C++, because variable-length arrays are a language extension.
With that part out of our way, consider what happens in your first loop's last step: you are accessing an element past the end of the array, which is undefined behavior.
Your second loop is correct, though, so all you need to do is correcting the header of the first loop to match the header of the second loop.
An array of N elements has indexes from 0 to N-1 (so: N-1 - 0 + 1 = N elements, some maths.)
So fix this: for(int i = 1; i<= N; i++) with this : for(int i = 1; i< N; i++)