White a function that swaps values using templates
int main ( ) {
int X=5
int Y=29 ;
double a = 1.0 ;
double b = 3.14 ;
swap ( X , Y ) ;
swap ( a , b ) ;
return 0 ;
}
I need you to explain it step by step
#include <iostream>
using namespace std;
template <typename T> // make this function templated
void swap(T &n1, T &n2) { // use T as type of parameters
T temp = n1; // set temp as n1
n1 = n2; // change n1 to n2
n2 = temp; // change n2 to original value of n1 using temp
}
int main() {
int X = 5;
int Y = 29;
double a = 1.0;
double b = 3.14;
swap(X, Y);
swap(a, b);
return 0;
}
White a function that swaps values using templates int main ( ) { int X=5 int...
Design a swap function with the following interface: void swap( int *x, int *y) { } In your main( ), you perform the following test: int main( ) { int a = 10, b = 20; cout << “a = “ << a << “ b= “ << b << endl; swap( a, b); cout << “a = “ << a << “ b= “ << b << endl; return 0; … }
MIPS assembly language
Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function
81. The following function call doesn’t agree with its prototype: cout << BoxVolume( 10, 5 ); int BoxVolume(int length = {1}, int width = {1}, int height = {1}); T__ F__ 82. The following function is implemented to swap in memory the argument-values passed to it: void swap(int a, int b) { int temp; temp = a; a = b; b = temp; ...
Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps X[k] and X[k+1] and returns 1; otherwise, it returns 0. You may use either conditional branch or conditional execution or their combination. int swap(unsigned short X[], int k) if (X[k] > X[k+1]) { short tmp = X[k]; X[k+1] = X[k]; X[k] = tmp; return 1; else { return 0;
1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...
Given the following main program: int main() { int x; int y = 10; int z = 20; x = add(y,z); return 0; } Which function is correctly written to store the value in x? int add(int a, int b) { return a+b; } float add(float a, float b) { float x; x = a + b; return x; } void add(int y, int z) { int x; x...
#include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij); printf("k=%d\n",k); return 0; int function] (int x, int y) int z; z=x*2+y; return z;
C++
1.
A?B?C?D? which one is correct
2.
3A, 3B
#include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...
Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...
C++
What is the result of the following program? #include <algorithm> int main() {int x = 10; int *y = &x; int *z = new int; z[0] = 20; std::swap (y, z); return x;} (A) Does not compile. (B) Runtime Error or Undefined Behavior (C) Returns 10 (D) Returns 20