Alan
Alan

Reputation: 13

Swapping values of two variables without using a third one- Array

I am writing a program which takes user input to determine pair of numbers and swaps values of all the pairs entered by the user.

For example:

User wants to enter 3 pairs, then he would enter 3 followed by pairs:

3 1 2 3 4 5 6

Output:

2 1 4 3 6 5

My program is giving proper output but instead of taking all pairs at once, it takes them one by one and gives the output. I have a vague idea that this can be resolved by using array but not sure how. Please help.

Here is my code:

#include <stdio.h>
int main()
{
    int x, y, p, i;
   //int a [100], b[100];
    printf ("How many pairs?\n");
    scanf ("%d", &p);
    for(i=1; i<=p; i++)
    {
       printf ("Enter two values:\n");
       scanf("%d %d",&x,&y);

       x = x + y;  
       y = x - y;  
       x = x - y;
      //a[i]=x;
      //b[i]=y;



      printf("\n");
      printf("After Swapping: x = %d, y = %d\n", x, y);
     }

     return 0;
}

Currently the output looks like:

How many pairs? 2

Enter two values: 2 3

After swapping x= 3 and y=2

Enter two values: 4 5

After swapping x= 5 and y=4. I want it to take all 4 values together and display output.

Upvotes: 1

Views: 2013

Answers (4)

Rajeev Atmakuri
Rajeev Atmakuri

Reputation: 888

Since you have added a C++ tag, I'll suggest a simple STL solution:

#include <iostream>
#include <vector>

int main(){
  std::vector<std::pair<int,int>> Vec;
  std::pair<int,int> Pr;
  int n;
  std::cin>>n;  //no. of pairs

  while(n--){
    std::cin>>Pr.second>>Pr.first;  //pair of elements stored in swapped locations
    Vec.push_back(Pr);
  }

  for(auto element : Vec){
    std::cout<<element.first<<" "<<element.second<<" ";
  }
  return 0;
}

Upvotes: 0

Sanfer
Sanfer

Reputation: 413

I'm assuming you're working on C so I've only used C headers. One way to do this is to first find out how many pairs there are, dynamically allocate space as is applicable, fill the array with the input integers, swap them, and then print them.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Enter numbers: ");

    /* Get the number of pairs. */
    int prs;
    scanf_s("%i",&prs);

    /* Create a dynamic array. */
    prs*=2;
    int *A = (int*) malloc(prs*sizeof(int));

    /* Read the integers into the array. */
    int i=0;
    for(;i!=prs;scanf_s("%i",&A[i++]));

    /* Swap. */
    int j=0;
    for(;j!=prs;j+=2)
    {
        int tmp = A[j];
        A[j] = A[j+1];
        A[j+1] = tmp;
    }

    /* Print. */
    for(i=0;i!=j;printf("%i ",A[i++]));
    printf("\n");
}

Cheers ! If you're interested in really getting good, try this book.

Upvotes: 0

Turbo
Turbo

Reputation: 2551

You can store them in an array (like you were trying to in some of the commented code), and then swap them one by one.

#include <stdio.h>
int main()
{
    int x, y, p, i;
    printf ("How many pairs?\n");
    scanf ("%d", &p);
    int a[p],b[p];
    for(i=0; i<p; i++)
    {
        printf ("Enter values for pair %d:\n", i+1);
        scanf("%d %d",&a[i],&b[i]);
    }
    for(i=0; i<p; i++)
    {
        x = a[i];
        y=b[i];
        x = x + y;  
        y = x - y;  
        x = x - y;
        printf("Pair %d After Swapping: x = %d, y = %d\n", i+1, x, y);
    }

     return 0;
}

Try it here: http://goo.gl/5JJ7i3

Upvotes: 0

dev.null
dev.null

Reputation: 538

I think, you are looking for something like this (you need only one array; you can directly store the values in swapped order):

for(i=0; i<p; i++) {
    scanf("%d %d",&x,&y);
    a[2*i+1]=x;
    a[2*i]=y;
}
for(i=0; i<2*p; i++) {
    printf("%d ", a[i]);
}
printf("\n");

Upvotes: 2

Related Questions