Programming Exercise 1: Simulating Call by Reference
Write a program that contains both a main function and a function called swap. Your main program should read in two integer values from the keyboard and then call the function swap.
The swap function will swap the original values and then return back to main. The function main will then output the values contained in the original variables. The original values should be swapped. No global variables are to be used. You must simulate call by reference by passing the address of the variables to the function swap. You must be able to do this. Do not use an array. I want you to practice using the address operator (&) and the dereference operator (*). You need to learn how to deal with pointers as a simulation of call by reference in C.

Programming Exercise 2: Malloc Up Space for One Integer
Write a C program that will malloc up space dynamically. The space should be able to hold exactly one integer value. After the space has been reserved, you should deposit the number 6 into the space. After the number 6 has been deposited into the space you should print the value that the space currently holds (that is, 6). You need to create a variable that contains a pointer to an int. Do not use an array index operator. Once you are done, you should free the allocated space explicitly.

Programming Exercise 3: Malloc Up Space for a One-Dimensional Array of n Integers
Write a program that will first read in a positive integer number, say n, from standard input. Then you need to create enough space in the heap via a call to malloc to hold an array of n integers. You then need to use array index operators to fill the array with n integer values entered by the user via calls to scanf. The index operator is also referred to as a subscript operator (i.e., A[i]). After the array has been filled with numbers, your program should walk down the array and build their sum. Print out the sum.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.


#include <stdio.h>

void swap(int *a, int *b);

int main(int argc, char* argv[]) {
    int a, b;   
    /* read in two integer values from the keyboard */
    scanf("%d", &a);
    scanf("%d", &b);
    /* and then call the function swap */
    swap(&a, &b);
    /* then output the values contained in the original variables */
    printf("%d %d", a ,b);
    return 0;

By purchasing this solution you'll be able to access the following files:

50% discount

$18.00 $9.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats