Using C/C++, declare three integer variables (e.g. A, B, C) in main () and assig
ID: 3813063 • Letter: U
Question
Using C/C++, declare three integer variables (e.g. A, B, C) in main () and assign then values from standard input (cin). Then, call a function swap 1 () to swap their values through pointers (e.g. A, B, C are 4. and 6, after calling swap(), A has B's value 5, B has C's value 6, and C has A's value 4), and output A, B, and C, finally, define another function swap2 to swap A, B, C one more time using references. Using C/C++, define an integer pointer in main (), and use this pointer to dynamically allocate an integer array with 50 elements; assign the array with random numbers in [0, 999]. Then, pass the array as parameter(s) to a function prime_count() which counts the number of the integers that are prime, and prints those integers; after the function call, print "counter" result. finally, use delete to deallocate the array memories.Explanation / Answer
#include <iostream>
using namespace std;
// Function prototype
void swap2(int&, int&,int&);
void swap1(int *,int *,int *);
int main()
{
int a, b,c;
cout<<"Enter a,b and c values: ";
cin>>a>>b>>c;
cout << "Before swapping" << endl;
cout << "a = " << a << ", b = " << b << " and c = " << c << endl;
swap2(a, b,c);//call by reference
cout << " After swapping by reference" << endl;
cout << " a = " << a << ", b = " << b <<"and c = "<< c << endl;
swap1(&a,&b,&c);
cout << " After swapping by pointer" << endl;
cout << " a = " << a << ", b = " << b <<"and c = "<< c << endl;
return 0;
}
void swap2(int& n1, int& n2,int &n3) {
int temp;
temp = n1;
n1 = n2;
n2 = n3;
n3=temp;
}
void swap1(int *a,int *b,int *c )
{
int *temp,i;
temp=&i;
*temp=*a;
*a=*b;
*b=*c;
*c=*temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.