Question

MAC 102 Quiz 2 Write a C++ programs to: 1. (2 points) receive two numbers from the keyboard, 2. (3 points) save these two num
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy

#include <iostream>
using namespace std;

/*
Below function is taking an array and swapping it's element.
Note:- No need to pass array as reference.
Arrays are always passed by reference.
  
Below swap function will successfully swap two numbers and
also changes will be there in main function.
*/

void swap(int arr[]){
  
//Temporary variable to hold array element
int temp;
//Performing swapping of array elements
temp = arr[0];
arr[0] = arr[1];
arr[1] = temp;
}

int main(){
  
int num1, num2;
//Array of two elements
int arr[2];
  
//Taking two numbers from keyboard
cout<<"Enter two numbers: ";
cin>>num1>>num2;
  
//Saving numbers in array of two elements

arr[0] = num1;
arr[1] = num2;
  
cout<<"\nArray elements before swapping: "<<arr[0]<<" "<<arr[1];
swap(arr);
cout<<"\nArray elements after swapping: "<<arr[0]<<" "<<arr[1];
  
return 0;
}

Program screenshot

1 #include <iostream> 2 using namespace std; 3 4- /* 5 Below function is taking an array and swapping its element. 6 Note:-

20 } 21 22. int main() { int num1, num2; //Array of two elements int arr[2]; V/Temporary variable to hold array element cout<

Program Output

input Enter two numbers: 20 40 Array elements before swapping: 20 40 Array elements after swapping: 40 20 .. Program finished

//Saving numbers in array of two elements arr[0] = num; arr[1] = num2; cout<<\nArray elements before swapping: <<arr[@]<<

Add a comment
Know the answer?
Add Answer to:
MAC 102 Quiz 2 Write a C++ programs to: 1. (2 points) receive two numbers from...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0,...

    i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0, -56, 4, -7 and array2-19, 12, -36, -2, 12 3. Write the code to form and display another array array3 in which each element is the sum of numbers in the position in both arrays. Use pointers and functions: get array0. process arrays0 and show array0 (50 points) 2. Enter a positive 5-digit integer via the keyboard. Display each digit and fol- lowed by...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • write the jave code for the following 1 2 7 B9 4 5 10 11 12...

    write the jave code for the following 1 2 7 B9 4 5 10 11 12 15 13 16 23 24 14 22 17 25 26 18 19 21 20 INRerscrorTro zo Question 11 15 points Save Answer Suppose we have the following code in a Java program: int numbers- (23, 2, 32, 13, 43, 67, 102, 19, 5, 88) System.out.printin ("Please enter a number: ") Scanner console - new Scanner (System.in): int numEntered - console.nextInt (): Add additional lines...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • cpp Lab Quiz 2 Time Slot: Mon 1-2 PM Deadline: 05/13/2019 2:00 PM Submission: Email Subject: CSC 102 CC3/CC4 LabQuiz 2TS 2 FileType: cpp Write a program to define a music player. Music player has...

    cpp Lab Quiz 2 Time Slot: Mon 1-2 PM Deadline: 05/13/2019 2:00 PM Submission: Email Subject: CSC 102 CC3/CC4 LabQuiz 2TS 2 FileType: cpp Write a program to define a music player. Music player has these features: isPlaying, isStopped, currentSongTitle, nextSong Title, currentSongLength. Programmer can play or stop a song. You can also choose to skip to the next song. Songs are stored in array of strings. When you choose to move to the next song, currentSong Title and nextSongTitle...

  • in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array...

    in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array filled up with random positive integer number ranging from 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following: [P]osition [Reverse, [A]verage, (Search, [Q]uit Please select an option: Then, if the user selects: -P (lowercase or uppercase): the program displays the array elements position and value pairs for all member elements of...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • c++ I am suppose to write a function that gets a number from a user, say...

    c++ I am suppose to write a function that gets a number from a user, say 3.123456789 and then the program gets another number from the user which is used to find out how many decimal digits we want rounded to. So if the user inputs 3 then the number would turn into 3.123 then we are suppose to add two digits next to the rounded number so it would turn into 3.12300. the rounding is suppose to be done...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT