Aug 11, 2018 at 1:09pm UTC
/* 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;
}
Aug 11, 2018 at 1:18pm UTC
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 Aug 11, 2018 at 1:18pm UTC
Aug 11, 2018 at 4:04pm UTC
I get this question from my lecturer. Can you help me
Aug 11, 2018 at 5:40pm UTC
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.
Aug 11, 2018 at 5:54pm UTC
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;
}
Aug 11, 2018 at 5:55pm UTC
Look at the input values.
Look at the output values.
Spot the pattern.
Aug 11, 2018 at 6:05pm UTC
Last edited on Aug 11, 2018 at 6:05pm UTC
Aug 11, 2018 at 6:08pm UTC
no didnt, actually im new in programming, thank you sir
Aug 11, 2018 at 6:09pm UTC
Running programs is a very important part of programming. You definitely need to learn how to run a program.
Aug 11, 2018 at 6:33pm UTC
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.