Exchanging values from vectors

First of all, this is my first post :)

The example is :
consider a vector that has 9 positions where each element (the value of them ) is the next position . (i know its not easy to understand , bare with me)
Position on the vector 1 2 3 4 5 6 7 8 9
Input: 5 7 6 9 2 8 4 0 3
output 5, 2, 7, 4, 9, 3, 6, 8, 0

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
27
28
29
30
31
32
33
  #include <stdio.h>

int vetor[9];
int contador = 0;
int i;

void fazercaralho()
{
	for (i = 0; i < 9 ; i++)
	{
		scanf("%d", &vetor[i]);
	}
	for (i = 0 ; i < 9 ; i++)
	{	
		contador = vetor[i] - 1;
		vetor[i + 1]= vetor[contador];
				
	}

	for (i = 0 ; i < 9 ; i++)
	{
		printf("%d",vetor[i]);	
	}



}
int main()
{
	fazercaralho();


}
Last edited on
1
2
for (i = 0 ; i < 9 ; i++)
   vetor[i + 1] //out of bounds 


¿what's your question?
I would recommend you to use another array for the result, you are overwritting important data in your code.

Also, limit the scope of your variables and check out parameters
consider a vector that has 9 positions where each element (the value of them ) is the next position

Please add further details
Position on the vector 1 2 3 4 5 6 7 8 9
                Input: 5 7 6 9 2 8 4 0 3
                output 5 2 7 4 9 3 6 8 0


Which operation should transform each value?
Position   current val    new val     operation
   1           5             5           ?
   2           7             2           ?
   3           6             7           ?
   4           9             4           ?
...


Note: shouldn't we refer to positions inside the array by their ‘real’ number, i.e. form 0 to 8 rather than from 1 to 9?
Topic archived. No new replies allowed.