Can You Shift an Array by a User-Inputted Value Using For Loops?

In summary, the conversation discusses using for loops to shift the index of an array based on a user inputted value. The code provided includes a printMessage function, a user input for choice, and a for loop that uses the value of b as the condition for the loop. However, upon compiling, the message is not outputted and it is discovered that the condition s == b does not make sense and is fixed to make the code work properly.
  • #1
AST3019
5
0
So I am trying to use for loops to shift the index an array by a user inputted value. The code i have so far is
printMessage();
cin >> choice;
if(choice == 2){
cout << endl;
cout <<"Please enter the desired number of shifts:" << endl;
cin >> b;
int s;
for (s = 0; s == b;s++) {
cout << message[s + b];
}

}
When I try to compile it, it doesn' cout the message. The input from the message occurs earlier in the code.
 
Technology news on Phys.org
  • #2
Have a think about why you put s == b in the for loop. It doesn't make any sense at all.
 
  • #3
yeah, right after i posted the thread i realized that and fixed it and now it works. thanks though.
 

Related to Can You Shift an Array by a User-Inputted Value Using For Loops?

1. What are shifting indices of an array?

Shifting indices of an array refers to the process of moving the elements within an array to different index positions. This can be done by either adding or removing elements, or by rearranging the existing elements.

2. Why would someone want to shift indices of an array?

Shifting indices of an array can be useful in many different situations. For example, it can be used to reorganize data, to sort the elements in a specific way, or to add or remove elements without altering the overall structure of the array.

3. How is shifting indices of an array different from sorting an array?

Shifting indices of an array involves moving the elements to different index positions, while sorting an array involves rearranging the elements in a specific order (e.g. ascending or descending). Shifting indices does not necessarily change the order of the elements, but sorting an array does.

4. Can shifting indices of an array cause data loss?

Yes, if shifting indices of an array is not done carefully, it can result in data loss. This can happen if elements are accidentally overwritten or removed during the shifting process. It is important to be mindful of this and to use caution when shifting indices.

5. What are some common methods for shifting indices of an array?

Some common methods for shifting indices of an array include using built-in functions such as array_shift() and array_slice(), or using loops to manually rearrange the elements. The specific method used will depend on the desired outcome and the programming language being used.

Similar threads

  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
14
Views
31K
Back
Top