Create this program.

Set the series 2,4,6....18,20. into the array.Then perform processes to move the number one position to the right and store the content of the last element into the first element to rearrange the siries to 20,2,4,6...18.
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


To fill array with values is a simple loop. http://www.cplusplus.com/reference/numeric/iota/
You need to store one value temporarily outside of the array.
Copying values to right is an another loop, but the direction of iteration is important.
thankss....
I have able to create the loop but..i still cant figure out how to bring the last number infront...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
	int num [10]={2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20 }; 
		for(int i=0;i<10;i++)
	{
		for(int i=0;i<10;i++)
		{
			cout<<num[i]<<"\t";
		}
		cout<<endl;
	
	}
	cout<<endl;
	return 0;
}
Use a temp var to store the last element , shift all the other elements 1 to the right, finally set the first element of the array to the temp var.

The easiest would be to use std::rotate but you are probably not allowed to. ?!
Why do you print the array 10 times?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using std::cout;
int main()
{
  int num [] = {2, 4, 6, 8, 10, 12 , 14, 16, 18, 20};

  for ( auto x : num ) cout << x << '\t';
  cout << '\n';

  // store the last value outside of the array:
  int last = num[ ??? ];

  // do something with the other values
  // You want to copy the 18 to where the 20 was
  // You want to copy the 16 to where the 18 was
  // You want to copy the 14 to where the 16 was
  // ...

  // return the stored value
  num[ ??? ] = last;

  for ( auto x : num ) cout << x << '\t';
  cout << '\n';

  return 0;
}
Last edited on
Yes,Im not allowed to do that.Im not allowed to use,,, "auto".
Yes,Im not allowed to do that.Im not allowed to use,,, "auto".

Ahh, no C++11 features. Very well,
1
2
3
4
5
6
7
for ( auto x : num ) std::cout << x << '\t';
std::cout << '\n';

// does same as
const int N = sizeof(num) / sizeof(int);
for ( int i=0; i < N; ++i ) std::cout << num[i] << '\t';
std::cout << '\n';


@TheSmallGuy:
No, that won't do. It does totally irrelevant and unnecessary operations.
Yes you are.
(First, it is not your assignment, just like you seemed to understand in your first gentleguy-copy post.)

You seem to search the largest value form the array and move it to the first element. That is not the same as "store the content of the last element into the first element".

You produce the result in a different array (arr_ret) that is ten times too large. The assignment expects "in-place" rearrangement that requires only one integer of additional memory.

Your code produces unexpected results if the maximum value occurs more than once.
Was finding the max element necessary in this problem? I don't think that was in the problem statement. Even if it was, it could have been done in the same loop that rearranged the elements since the movement didn't depend on the max element.

Anyway, @TheSmallGuy: Quit giving out answers and try to lead them to the solution instead. Just giving someone a solution does little to help them learn.

Just one loop and one value remembered outside the array, like keskiverto said, but be careful about which way you go through the array.
@TheSmallGuy
There definitely is, and having a concept click is the icing on that cake. Don't take away the icing. Interview Q: "Why does this do what it does"? Answer: "Oh, I don't know why, I just know that it works".

This post made no mention of compiler errors, nor did I really see anything that would immediately cause a compiler error. If you do address issues, you should explain why the other way was wrong instead of just spitting out code. Reference the above crappy answer to an interview question.

You can still solve the problem in less code than you gave. I did it in 3 lines. You don't need a secondary array to hold the moved elements. You can move them in-place if you iterate through them the right way.
I agree with JayhawkZombie

Realise that TheSmallGuy (aka gentleguy, gentlgal and probably dozens of others) are all trolls. It's possible that the OP in posts they are involved in are also trolls. Not saying that this is the case for this particular topic, but it is possible.

Sorry for this attitude, I am very sceptical about a lot of posts on this site these days.
Topic archived. No new replies allowed.