Question

In: Computer Science

The following question must be answered in the C programming language and may not be written...

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.

Solutions

Expert Solution

#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

*/


Related Solutions

C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
The following is for C programming language: I want to scan for initials in a line...
The following is for C programming language: I want to scan for initials in a line of text. my line of text is as follows: 12345 3.5000 a j 12346 4.1000 s p The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code: fscanf(fp, "%d %c %c", &studentID,...
“Computer programming is creating a sequence of very precise instructions written in a language a computer...
“Computer programming is creating a sequence of very precise instructions written in a language a computer understands, to perform a specified task with a computer.” Discuss in detail the concept of extreme precision in computer programming.
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write...
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write a simple assembly language program that performs a few arithmetic operations. This will require you to establish your programming environment and create the capability to assemble and execute the assembly programs that are part of this course. Your \student ID number is a 7-digit number. Begin by splitting your student ID into two different values. Assign the three most significant digits to a variable...
I have a question about C++ programming Language class. I am confused with these terms and...
I have a question about C++ programming Language class. I am confused with these terms and what they are for. 1. Unix 2. Terminal 3. Git 4. CLOC 5. Linux Please explain each words and what's their jobs for C++. Also, if you know some good sources/websites, could you please provide me a link where I can learn how to use Unix, Terminal, Git, etc.
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be...
C PROGRAMMING Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] a) if ( sales => 5000 ) puts( "Sales are greater than or equal to $5000" ) else puts( "Sales are less than $5000 ) b) int x = 1, product = 0; while ( x <= 10 ); { product *= x; ++x; } c) While ( x <= 100 ) total =+ x;...
Complete the following assignment in C programming language. Get the user’s first name and store it...
Complete the following assignment in C programming language. Get the user’s first name and store it to a char array Declare a character array to hold at least 20 characters. Ask for the user’s first name and store the name into your char array. Hint: Use %s for scanf. %s will only scan one word and cannot scan a name with spaces. Get 3 exam scores from the user: Declare an array of 3 integers Assume the scores are out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT