Please help me to solve this problem. Thank you for your cooperation :)

/* Q1
* Read the functions below and try to understand what function_without_name_a is trying to.
* Once you understand what it is doing, give this function a name relevant to what it is doing.
* */
#include <iostream>

using namespace std;

void printArr(int* arr,int count){
for(int i=0;i<count;i++){
cout<<arr[i]<<",";
}
cout<<endl;
}


void function_without_name_a(int* inputArray){
int tmp;

tmp = inputArray[0];
inputArray[0] = inputArray[1];
inputArray[1] = tmp;

printArr(inputArray,2);
}

int main(){

int a[2] = { 33, 103};
int b[2] = { 103, 500};
int c[2] = { 45103, 500};

function_without_name_a(a);//103,33,
function_without_name_a(b);//500,103,
function_without_name_a(c);//500,45103

return 0;
}
function_without_name_a(a);//103,33,
function_without_name_a(b);//500,103,
function_without_name_a(c);//500,45103


Isn't it obvious from this part?
Last edited on
I get this question from my lecturer. Can you help me
Input: 33, 103
Output: 103, 33

Input: 103, 500
Output: 500, 103

Input: 45103, 500
Output: 500, 45103

Take a good look at those and see if you can spot the pattern.
Thank you so much. How about this?


#include <iostream>

using namespace std;

void printArr(int* arr,int count){
for(int i=0;i<count;i++){
cout<<arr[i]<<",";
}
cout<<endl;
}

void function_without_name_b(int* inputArray,int arrLength)
{
bool swapped = false;
do {
swapped = false;
for (int i=0; i < (arrLength-1); i++) {
if (inputArray[i] > inputArray[i+1]) {
int temp = inputArray[i];
inputArray[i] = inputArray[i+1];
inputArray[i+1] = temp;
swapped = true;
}
}
} while (swapped);
printArr(inputArray,arrLength);
}

int main(){
int data0[9] = {33, 103, 3, 726, 200, 984, 198, 764, 9};
int data1[10] = {34, 32, 143, 323, 16, 2240, 934, 1, 74, 1};
int data2[9] = {3433, 3103,43, 7236,200, 984, 98, 764, 9};

function_without_name_b(data0,9);
function_without_name_b(data1,10);
function_without_name_b(data2,9);

return 0;
}
Look at the input values.
Look at the output values.

Spot the pattern.
Whats the answer sir?
Did you run the code?

http://cpp.sh/92bnc

Look at the output values.
Last edited on
no didnt, actually im new in programming, thank you sir
Running programs is a very important part of programming. You definitely need to learn how to run a program.
I am confused by the question given. "give this function a name relevant to what it is doing". I thought the question ask me to give a name for that function.
give this function a name relevant to what it is doing


So first, you need to work out what the function does.

Then, you give it a name.

For example, if the function multiplied everything by two, you might name it "multiply_everything_by_two".

Topic archived. No new replies allowed.