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.
#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!
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] );
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?
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.
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.