Hi friends
I need the one dimensional array rotating program
that is
the array element is
012345
123450
234501
345012
etc
Printable View
Hi friends
I need the one dimensional array rotating program
that is
the array element is
012345
123450
234501
345012
etc
Announcements - General Programming Boards
Announcements - C++ Programming
Announcements - General Programming Boards
Since you missed them first time around.
You need to show some effort.
You can't just say "I need" and leave it at that.
please give the logic
i am not an experts
Code:#include <iostream>
using namespace std;
int array[]={1,2,3,4},temp[10],i,l,j;
int main()
{
for(j=0; j<3; j++)
{
std::cout<<array[j]<<std::endl;
}
cout<<std::endl;
for(l=0;l<3; l++)
{
for(i=0; i<3; i++)
{
temp[i] = array[i];
array[i]= array[i+1];
std::cout<<array[i]<<std::endl;
}}
std::cout<<array[1]<<std::endl;
return 0;
}
First, you need to indent your code better, if you hope to "see" the logic.
Second, think about some of the steps involved, and try to solve each one in turn.Code:#include <iostream>
using namespace std;
int array[] = { 1, 2, 3, 4 }, temp[10], i, l, j;
int main()
{
for (j = 0; j < 3; j++) {
std::cout << array[j] << std::endl;
}
cout << std::endl;
for (l = 0; l < 3; l++) {
for (i = 0; i < 3; i++) {
temp[i] = array[i];
array[i] = array[i + 1];
std::cout << array[i] << std::endl;
}
}
std::cout << array[1] << std::endl;
return 0;
}
Eg.
Can you see how this solves part of the problem (don't just look, run the code)?Code:#include <iostream>
using namespace std;
int main()
{
const int SIZE = 9;
int array[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, j;
// before
for (j = 0; j < SIZE; j++) {
std::cout << array[j] << " ";
}
cout << std::endl;
for (j = 0; j < SIZE-1; j++) {
array[j] = array[j+1];
}
// after
for (j = 0; j < SIZE; j++) {
std::cout << array[j] << " ";
}
cout << std::endl;
return 0;
}
When you see it, can you then think how to get the 1 from the start to the end?
But sir by this code there is no rotating the output is as follows
123456789
234567899
the 9 is repeating
the last 9 is needed to change as 1 thus rotating .thanks for the code
The code example was meant to help you along, not provide you with a completely working solution. So, what can you do about this observation?Quote:
Originally Posted by thannara123
Okay thanks for helping i am trying to get my need i will post soon