In: Computer Science
The following question must be answered in the C programming language and may not be written in C++ or any other variation.
Problem 5
This problem is designed to make sure you can write a program that swaps data passed into it such that the caller's data has been swapped. This is something that is done very frequently in manipulating Data Structures.
The Solution / Test Requirements
I want your program to demonstrate that you understand how to swap information passed into a function and have the calling routine print out the data before and after the call to verify the data has been swapped. Call your functions Swap and SwapStructs. Swap is a function that receives the information to swap. The information is two integers. How should Swap receive them? How should it process them? SwapStructs is a function that receives two structs to swap. How should SwapStructs receive them? How should it process them?
Your Test Main must:
1. Declare two integers and initialize them to the values 1 and -1.
2. Print out the integers.
3. Call Swap to swap the values.
4. Print out the values on return from the function.
5. Declare a struct that holds 2 integers using a typedef for the struct.
6. Dynamically allocate two separate variables using the typedef and fill the dynamically allocated structs, filling the first with the values 10 and 20 and the second with 30 and 40.
7. Print out the values in each of the structs
8. Call SwapStructs to swap the contents of the structs.
9. Print out the values in the structs on return.
10. Free the memory that was dynamically allocated.
For your convenience, here is an output of my program:
Before call..I= 1, J=-1 After call.. I=-1, J= 1
Before SwapStructs..ptr1 contains 10 and 20
Before SwapStructs..ptr2 contains 30 and 40
After SwapStructs..ptr1 contains 30 and 40
After SwapStructs..ptr2 contains 10 and 20
Process returned 0 (0x0) execution time : 0.034 s Press any key to continue.
#include <stdio.h>
typedef struct Point{
int x;
int y;
}point;
void swap(int *a,int *b) //swap should recieve variable address and
store them in pointers.
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void swapStruct(point * p1, point *p2)
{
swap(&p1->x,&p2->x);
swap(&p1->y,&p2->y);
}
int main(void)
{
int a=1,b=-1;
printf("Before call...a=%d,b=%d\n",a,b);
swap(&a,&b); //pass the adress of the
variables.
printf("After call...a=%d,b=%d\n",a,b);
point *p1=(point*)malloc(sizeof(point));
point *p2=(point*)malloc(sizeof(point));
if(!p1 || !p2)
{
printf("memory allocation
failed\n");
return -1;
}
p1->x=10;
p1->y=20;
p2->x=30;
p2->y=40;
printf("Before SwapStructs..ptr1 contains x=%d and
y=%d\n",p1->x,p1->y);
printf("Before SwapStructs..ptr2 contains x=%d and
y=%d\n",p2->x,p2->y);
swapStruct(p1,p2);
printf("After SwapStructs..ptr1 contains x=%d and
y=%d\n",p1->x,p1->y);
printf("After SwapStructs..ptr2 contains x=%d and
y=%d\n",p2->x,p2->y);
free(p1);
free(p2);
return 0;
}
/*
Before call...a=1,b=-1
After call...a=-1,b=1
Before SwapStructs..ptr1 contains x=10 and y=20
Before SwapStructs..ptr2 contains x=30 and y=40
After SwapStructs..ptr1 contains x=30 and y=40
After SwapStructs..ptr2 contains x=10 and y=20
*/