char ph[6]={'a','b','c','d','e','f'};
that is my code and this is how I summon them
for(int i=1;i>=3;i++)
{
cout<<ph[i]<<" ";
}
but it turn out blank
Your loop is never going to run.
for(int i=1;i>=3;i++)
Means start at 1 and while we are greater then 3 continue the loop. Since we are never greater than 3 the loop ends.
If you want to print the first 3 elements of the array, then you would use:
for(int i = 0; i < 3; i++)
cout<<ph[i]<<" ";
Related
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);
So, I tried to make an array using input first, then sorting it out from smallest to biggest, then display the array to monitor.
So I come up with this code :
#include <iostream>
using namespace std;
void pancakeSort(int sortArray[], int sortSize);
int main()
{
// Input The Array Element Value
int pancake[10];
for(int i=0; i<10; i++)
{
cout << "Person " << i+1 << " eat pancakes = ";
cin >> pancake[i];
}
// call pancake sorting function
pancakeSort(pancake, 10);
}
void pancakeSort(int sortArray[], int sortSize)
{
int length = 10;
int temp;
int stop = 10;
// this is where the array get sorting out from smallest to biggest number
for(int counter = length-1; counter>=0; counter--)
{
for(int j=0; j<stop; j++)
{
if(sortArray[j]>sortArray[j+1])
{
temp = sortArray[j+1];
sortArray[j+1] = sortArray[j];
sortArray[j]=temp;
}
}
stop--;
}
// after that, the array get display here
for(int x=0; x<sortSize; x++)
{
cout << sortArray[x] << " ";
}
}
but the output is weird :
enter image description here
the function is successfully sorting the array from smallest to biggest,
but there is 2 weird things :
1. The biggest value element (which is 96 from what I input and it's the 10th element after got sorted out), disappear from the display.
2. For some reason, there is value 10 , which I didn't input on the array.
So, what happened?
In the loop
for(int j=0; j<stop; j++)
{
if(sortArray[j]>sortArray[j+1])
{
temp = sortArray[j+1];
sortArray[j+1] = sortArray[j];
sortArray[j]=temp;
}
}
stop is the length of the array, and you are iterating through values of j = 0 to stop - 1. When j reaches stop - 1, the next element that is j+1 becomes stop (10 in this case). But since your array has a length of 10, sortArray[10] is not part of the array, but is referring to some other object in memory which is usually a garbage value. The garbage value is 10 in this case. When you swap sortArray[10] and sortArray[9], the garbage value becomes part of the array and the value at index 9 leaves the array. This keeps on happening till the outer loop ends.
The end result is that unless the garbage value < largest element in the array, the garbage value is pushed in the array and the greatest value of the array is put at sortArray[10] which is not part of the array. If the garbage value is greater than all the values of the array, it'll be found at sortArray[10] which is again not part of the array and your code will return the desired result.
Essentially, what you are doing is giving the function an array of 10 (or stop) elements, but the function is actually working with an array of 11 (or stop + 1) elements, with the last element being a garbage value. The simple fix is to change the conditional of the loop to j < stop - 1.
Note that if you had written this code in a managed (or a comparatively higher level) language like Java or C#, it would have raised an IndexOutOfBoundsException.
At index 9, j+1 is out of bounds. So to fix this, you only need to check till index 8
for(int counter = length-1; counter>=0; counter--)
{
for(int j=0; j<stop-1; j++)
{
if(sortArray[j]>sortArray[j+1])
{
temp = sortArray[j+1];
sortArray[j+1] = sortArray[j];
sortArray[j]=temp;
}
}
stop--;
}
Look carefully at the inner loop condition j<stop-1
I am attempting to fill an array backwards from 20 to 0 but whenever I print it out it still prints out forwards. For instance I want to put in 1,2,3,4,5 and have it come out as 5,4,3,2,1.
I have attempted to do a for loop that counts backwards from 20 to 0 but when i print it it is still coming out incorrect. Any help?
int temp;
for (int i = 20; i > 0; i--)
{
cout << "Please enter the next number. Use a -1 to indicate you are done: ";
cin >> temp;
while(temp > 9 || temp < -2)
{
cout << "You may only put numbers in 0 - 9 or -1 to exit. Please enter another number: ";
cin >> temp;
}
arr1[i] = temp;
cout << arr1[i];
}
for (int i = 21; i > 0; i--)
{
cout << arr1[i];
What's the size of your array?
Assume that the size is 21 (indexes from 0 to 20).
First of all please note that your first loop will never populate the array at index 0 (something like this arr1[0] = temp will never be executed inside your first loop).
If you want to avoid this behavior you should write your first for loop like this:
for (int i = 20; i >= 0; i--){...}.
The second for loop has some issues:
You are traversing the array backwards while you want to do the opposite.
The loop starts from an index out of bound (21).
The loop may print some undefined values (You should remember the index of the last added value).
I suggest you to use other data structures like a Stack but if you want to use an array you can edit your code as follows:
int i;
for (i = 20; i >= 0; i--){...}
for (i; i <= 20; ++i) { cout << arr1[i]; }
If you don't want to declare int i; outside of the loop you can do something like that:
int lastAdded;
for (int i = 20; i >= 0; i--){
...
lastAdded = i;
}
for (int i = lastAdded; i <= 20; i++) { cout << arr1[i]; }
Edit: Note that neither your code nor mine stops asking for a new value after the insertion of a -1.
If you want to achieve this behavior you should use a while loop instead of the first for loop and check for the exit condition.
I'm having problem with displaying the sentence array outside of for loop
int example(string sentence)
{
int i = 0;
for (i; 1 < 50; i++)
{
cout<<sentence[i]<<endl;
}
cout<<sentence[i]<<endl;
return 0;
}
outside of for loop, sentence[i] isn't showing the character at sentence[49]
can someone tell me the reason?
Try this
int example(string sentence)
{
int i = 0;
for ( ; i < 50; i++)
{
cout<<sentence[i]<<endl;
}
cout<<sentence[i]<<endl;
}
You want to check if i is less than 50, not if 1 is less than 50, because that would always be true and the loop never terminates. Also, in the snippet I posted, i will have the value of 50 after the loop, so there better be a 51st element (0-based array).
The reason that it isn't displaying sentence[49] is that i is 50 after the loop runs. The i++ in your loop increments i after the loop iteration has run. So when i is 49 it will run through the loop and and then increment i to 50. Then it comes around to the conditional i < 50 and it sees that i is now 50 and the loop terminates. Then when you call sentence[i] outside of the loop it is trying to display sentence[50]. To get at the last character in sentence I would suggest one of the following:
cout << sentence[i - 1] << endl;
or the much easier to read:
cout << sentence.back() << endl;
My program is supposed to take in a number from user input, determine whether or not it is prime, and then if it is not, output the factors of the entered number, 5 to a line. The 5 to the line part is where everything goes haywire, the loop i wrote should work fine as far as i can tell, however no matter how much i change it around, it does one of two things, 1) goes infinite with either new lines or the first factor, or 2) outputs a line with 5 of each factor. Here's the code:
else
{
cout << "\nNumber is not prime, it's factors are:\n";
for (int x = 2; x < num; x++)
{
factor=num%x;
if (factor==0)
{
int t=0;
cout << x << "\t";
t++;
for (int t; t <= 5; t++) // THE TROUBLE LOOP
{
if(t>=5)
{
t=0;
cout << endl;
}
}
}
}
}
Replace the declaration of t in the loop since you've declared t prior to the loop:
for(; t <= 5; t++)
With int t in the loop declaration you are overriding t as an uninitialized variable that will have a garbage value.
Outside of this problem your loop is infinite since you will be resetting t to 0 whenever it equals 5.
In the for loop change the
int t
to
t=0
it is the
for(int t,t<=5,t++)
the int t part in particular that is causing the issue.
#GGW
Or this:
int t = 0;
//some code
for(t; t <= 5; t++)
//more code