Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please i need someone to help me perform a detailed analysis of the differences

ID: 3865485 • Letter: P

Question

Please i need someone to help me perform a detailed analysis of the differences and similarities between assignment M1 and M2 below, Thank you

M1

#include <iostream>

#include <conio.h>

#include <string>

#include "M7.h"

using namespace std;

void main()

{

       string sName;

       const int iArraySize = 5;

       int iArray [iArraySize] = {0};

       float fArray[iArraySize] = {0.0};

       ProgramHeader();//function call for the program header

      

       menu(sName, iArray, fArray, iArraySize);// the menu

       system("cls");//clearing the DOS window

       Output(sName);//outputing the char array

       Output(iArray, iArraySize);//outputing the int array while calling the sort and finding the median

       Output(fArray, iArraySize);//outputing the float array while calling the sort and finding the median

       cout << " press anyKey to end program...";

       _getch();

}//end main

M2

#include "stdafx.h"

#include<iostream>

#include<string>

using namespace std;

inline void nameInputFunction(string name)

{

getline(cin, name);

}

//below is a template function to take user input for an array

//and array is passed here as a pointer

template<typename T>

void functionForArrayInput(T *array)

{

for (int i = 0; i < 5; ++i) //size of array is known and it is 5

cin >> array[i];

}

template<typename T>

void sortArray(T *arr) {

int i, j;

T key;

for (i = 1; i < 5; i++)

{

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

{

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

}

void display(char name[]) {

cout << "User name is : ";

int i = 0;

while (name[1] != '') {

cout << name[i];

++i;

}

cout << endl;

}

void display(int array[]) {

cout << "Enterd integer array is : ";

int i = 0;

while (i < 5) {

cout << array[i] << " ";

++i;

}

cout << endl;

}

void display(float array[]) {

cout << "Enterd float array is : ";

int i = 0;

while (i < 5) {

cout << array[i] << " ";

++i;

}

cout << endl;

}

int main()

{

string name;

int int_array[5];

float float_array[5];

cout << "Enter user name : ";

nameInputFunction(name);

cout << "Please enter 5 element of int array : ";

functionForArrayInput(int_array);

cout << "Please enter 5 element of float array : ";

functionForArrayInput(float_array);

sortArray(int_array);

sortArray(float_array);

cout << "Your entered int array in sorted order is : ";

display(int_array);

cout << "Your entered float array in sorted order is : ";

display(float_array);

cout << "Median of both array is 3rd element in sorted array ";

cout << "Median of int array is : " << int_array[2] << endl;

cout << "Median of float array is : " << float_array[2] << endl;

return 0;

}

Explanation / Answer

To analyze the code better we have to understand what each function in the code is doing. So i am taking an approach of explaining each function and overall what code is trying to achieve. Hope this is helpful. If you have any concerns, please comment. I would be happy to clarify your doubts.

Lets start with M2 first:
There are 5 functions in M2. Below are their details on what they do.

nameInputFunction(name) : It will take input string from the user the function name.

functionForArrayInput(array): This is a generic function which can take either int, float or any other type function. In the assignment M2 it takes 5 integer elements and then 5 float elements as input from user.

sortArray(array): sortArray is another generic function where it can take any type array and sorts the array elements. Since arrays are passed by reference internally. After the sorting is finished, elements will be in sorting order. In the assignment it takes a integer array first sorts the elements and then takes a floating elements array and sorts them.

display(array): there are two functions for displaying array elements in M2. One is for integer and another is for float elements. Both the functions will print the given elements in array.

the last 3 cout statement actually prints the median of array in a 5-element sorted array which is middle element i.e. array[2]. It prints the median for both integer and float.

Overall, M2 assignment takes a function name as input. Then input 5 integer and float elements from the user. After that sorts both integer and float array and finally displays the given array in sorting order. In the end it prints median of integer and floating sorted array.

M1 analysis:

On a high level M1 seem to be using functions available in M2 to complete its work. Following are it functionalities.

menu(sName, iArray, fArray, iArraySize) : Looks like it displays set of functions as menu.
where sname menu option seem to be like asking function name as input from the user. It is similar to nameInputFunction(name) in M2.
iArray menu option seem to be taking integer array elements as input. It is similar to functionForArrayInput(array)
fArray menu option seem to be taking floating array elements as input. It is similar to functionForArrayInput(array)
iArraySize seem to represent array size which is 5

Output(sName) : it displays the given string which is similar to "void display(char name[])" in M2. But in M2 we cannot see given string is displayed. Thats one difference. We can see only printing integer or floating elements array but not the given user name as input.

Output(iArray, iArraySize) : Seems like sorting the 5 element integer and finding out median. It is similar to sortArray function in M2 and is similar to display(int array[]) for printing out integer array.

Output(fArray, iArraySize) : Seems like sorting the 5 element floating values and finding out median. It is similar to sortArray function in M2 and is similar to display(float array[]) for printing out floating array.

Overall M1 is moreover seem to be using utilities available in M2 to perform its function.