Question

Dynamically allocate memory for two integers. Swap their values. write your statement in C

Dynamically allocate memory for two integers. Swap their values. write your statement in C

Homework Answers

Answer #1
#include <stdio.h>
#include <stdlib.h>
int main(){
   // Dynamically allocate memory for two integers
   int *x = (int*)malloc(sizeof(int));
   int *y = (int*)malloc(sizeof(int));
   int temp;
   
   *x = 5;
   *y = 3;
   
   temp = *x;
   *x = *y;
   *y = temp;
   
   printf("*x = %d\n",*x);
   printf("*y = %d\n",*y);
   return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Write a Java swap method that could swap values of two integer variables. Do not use...
Write a Java swap method that could swap values of two integer variables. Do not use any predefined method. Also write a method call that swaps the values of the following two variables. int first = 7, second = 5;
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
state a vector of integers named val. Then write a for loop to push the values...
state a vector of integers named val. Then write a for loop to push the values 1 through 5 into the elements of the vector stated
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the...
Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the linked list) that will return a sum of all values.
Determine all values of n for which the following statement is true: There exists integers x...
Determine all values of n for which the following statement is true: There exists integers x and y such that 63x + 147y = n. Give a convincing argument to justify your answer.
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their...
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT