Help Transposing arrays.

Hi everyone I'm pretty new to C++ and I'm still getting my head round a lot of the logic behind it all. I need a bit of help with this task:

1. Create an array with 8 integers populated by the keyboard and label this with a meaningful name. This will be your first array.
2. Create a second array with another 8 integers this will also be populated by the keyboard.
3. Modify the program so that the contents of the first array are transposed to the second array and likewise the second array contents are transposed to the first array.


Here's the code I have so far:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

int main()
{
    int set1[8];
    int set2[8];
    int count1 = 0;
    int count2 = 0;
    int pcount1 = 0;
    int pcount2 = 0;
    
    {
    while(count1 < 8)
                 
                 
                 //Input for first array
                 
                 {
                 printf("\nPlease enter integer for the first array, location %d: " ,count1);
                 scanf("%d", &set1[count1]);
                 count1++;
                 }
                 }
                 system("cls");
    
    while(count2 < 8)
    
    
                 //Input for second array
    
                 {
                 
                 printf("\nPlease enter integer for the second array, location %d: " ,count2);
                 scanf("%d", &set2[count2]);
                 count2++;
                 }
                 
                 {
                 
                                
                 system("cls");
                 printf("Set A Contains:\n\n");
                 
    
                 //Print set A
                 
                 while(pcount1 < 8)
                 
                 {
                        printf("      %d\n", set1[pcount1]);
                        pcount1++;
                        
                        
                 }
                 
                 printf("Set B Contains:\n\n");
                 
                 
                 //Print set B
                 
                 while(pcount2 < 8)
                 
                 {
                        printf("      %d\n", set2[pcount2]);
                        pcount2++;
                               
                 }
                               
                 printf("Press any key to exit");
                 system("pause > nul");
                                  
                
}
}


As you can see I am able to populate the arrays and print them on screen, but I'm stuck at how to transpose the arrays now. I'm not entirely sure what my tutor means by that if I'm honest, we don't get any help from him. He literally just gives us these worksheets and we are expected to teach ourselves everything!

Any hints/clues/suggestions greatly appreciated!

Thanks.
1-look up for loops.

2- when defining a set of variables that are the same type do it with commas instead of new lines.
int a, b, c, d;

3-use std::cout instead of printf() because cout is easier and is considered the standard.

4- do not use system commands such as system("pause > nul");, you also forgot a return value for your function.
hehe.

I know ALL OF THESE THINGS, but this is just the way we are taught and practicing it... don't ask me why!

Thanks for the help though.....

If you're being taught this way... good god.
If you want to simply swap the arrays, then make a temp array to hold values, then put values of second array into first array, then values of temp array into second array.
you can swap one element from the first array with other element from the second array. This task is done with standard algorithm std::swap. For example


for ( int i = 0; i < 8; i++ ) std::swap( a[i], b[i] );
Last edited on
^ vlad has given you the preferred method.
Thank you everyone for your help. I have included that for loop now and can swap set1 with set2. But I'm struggling to understand something: With this code, set1 is now empty and set2 contains the contents of set1. How do I put the original contents of set2 into set1?

Edit: Okay so the swap command SWAPS the values with each other so the sets should be transposed. However, when printing the values again, set1 is empty but set2 contains the values of set1. Any suggestions?
Last edited on
Okay here's what I have:

1
2
3
for ( int x = 0; x < 8; x++ ) std::swap( set1[x], tempset[x] );
for ( int x = 0; x < 8; x++ ) std::swap( set2[x], set1[x] );
for ( int x = 0; x < 8; x++ ) std::swap( tempset[x], set2[x] );


I know something's wrong with it because I'm not able to print set2. It seems the set2 is empty by the end of this.
Last edited on
Any help at all?
tempset is completely unnecessary. Do it like vlad showed you.
If after the operation, set1 is "empty" as you call it, then that just means set2 was "empty".
Yeah benny. Think about your code logically. What is the point of tempset? If you are swapping values. The tempset is done for you inside the swap function, hence the function name.
Last edited on
Topic archived. No new replies allowed.