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...
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,...
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
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...
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...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT