Question

In: Computer Science

Problem 5 This problem is designed to make sure you can write a program that swaps...

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>

#include<iostream>

#include<malloc.h>

using namespace std;

void swap(int*,int*);

struct twoVar

{

int a;

int b;

};

typedef struct twoVar TwoVar;

void swapStructs(TwoVar*,TwoVar*);

int main()

{

int x=1,y=-1;

TwoVar *x1,*x2;

printf("\nbefore swapping x=%d and y=%d",x,y);

swap(&x,&y);

printf("\nafter swapping x=%d and y=%d",x,y);

x1=(TwoVar*)malloc(sizeof(TwoVar*));

x1->a=10;

x1->b=20;

x2=(TwoVar*)malloc(sizeof(TwoVar*));

x2->a=30;

x2->b=40;

printf("\nbefore swapping struct elements are x1->a=%d and x1->b=%d\nand x2->a=%d and x2->b=%d",x1->a,x1->b,x2->a,x2->b);

swapStructs(x1,x2);

printf("\nbefore swapping struct elements are x1->a=%d and x1->b=%d\nand x2->a=%d and x2->b=%d",x1->a,x1->b,x2->a,x2->b);

return 0;

}

void swapStructs(TwoVar* p,TwoVar* q)

{

TwoVar temp;

temp=*p;

*p=*q;

*q=temp;

}

void swap(int*p1,int*p2)

{

int temp=0;

temp=*p1;

*p1=*p2;

*p2=temp;

}


Related Solutions

Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Write 5+ pages in one of the following topics :- ( MAKE SURE THE HANDWRITING IS...
Write 5+ pages in one of the following topics :- ( MAKE SURE THE HANDWRITING IS READABLE, * I prefer to be made in M.S word ) 1- Contaminant transport in groundwater 2- Modeling of contaminant transport in groundwater
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and then reads through the file, counts the number of times each word appears and outputs the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint: use the try/except...
solve the problem make sure to explain in words what you did with the problem and...
solve the problem make sure to explain in words what you did with the problem and state your conclusions in terms of the problem. Part I: Choose to do one of the following: 1) Test the claim that the mean Unit 3 Test scores of data set 7176 is greater than the mean Unit 3 Test scores of data set 7178 at the .05 significance level Class 7176 Class 7178 Unit Test 3 Course Grade Attendance Unit Test 3 Course...
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5...
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop. Program #2 – Using both S1 and S2 1 Write a program like 1(b) above, but the LED1 will blink until S2 is pressed. 2 Write a program that simulates a motor coming up to speed. When S1 is pressed, LED1 blinks at 50% duty cycle for 10 cycles (indicating a motor coming up to speed)....
Write a simple MIPS Assembly program to average 3 integers. Make sure to read in three...
Write a simple MIPS Assembly program to average 3 integers. Make sure to read in three numbers from the user instead of hard coding them. To divide the sum by 3 you may use div $t0, $t0, 3 where register $t0 conatins the sum of the 3 integers. .data prompt1: .asciiz " Please enter an integer: " prompt2: .asciiz " Please enter an integer: " prompt3: .asciiz " Please enter an integer: " result: .asciiz "The average of three number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT