Question

Question 2 In this question, you will read two data files that include integers into two different arrays the same way we did5. Call the compare Order function from main passing your two arrays and their corresponding dsizes and give appropriate mess

Question 2 In this question, you will read two data files that include integers into two different arrays – the same way we did in class (but we are doing to arrays here). Duplicates are ok. 1- After you read the data into the array (use one function that takes an int array and a dsize by reference just like we did in class, and call that from main to fill both arrays). 2- Include a printArray function so that you could print the two arrays. Use array size 1000. 3- Use a sort algorithm to sort both arrays. 4- Now write a compareOrder function that takes two arrays and their data sizes. If the data size is the same and every element in the first array is equal to element in the same corresponding position in the second array (both sorted) – then the arrays are equal (return 0). If the very first element that is different is larger in one array than the other array then the first array is larger (return 1) otherwise (return -1). If the dsizes are different, the longer array is larger (return 1 if array1 is larger than array 2 and -1 otherwise). 5- Call the compareOrder function from main passing your two arrays and their corresponding dsizes and give appropriate messages. (Please see Hint - required). 6- Now write a compareSum to compare the two arrays based on the sum of their elements – follow the same theme of returning 0,1 or -1 – and call from main with appropriate messaging (please see Hint - required). Hint: Instead of calling compareOrder and CompareSum and including logic code for messaging in main (ugly), write a void wrapper function compare that would take a compare type, the arrays and their dsizes and will call the appropriate compare function with the arrays and the dsizes and produce the appropriate messaging).

Submit your cpp files pasted in a word document. Paste the code. Submit the data files you used separately as files not pasted in the word document. Include a screenshot of every output for each case

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

void ReadData(string file,int arr[],int &n)

{

       ifstream f;

       int a;

       f.open(file);

       if (!f)

       {

              cout << "Unable to open file";

              exit(1);

       }

       while (f >> a)

       {

              arr[n] = a;         

              n++;         

       }     

}

void printArray(int arr[], int size)

{

       cout << "The elements of the array: " << endl;

       for (int i = 0; i < size; i++)

       {

              cout << arr[i] << " ";

       }

       cout << endl;

}

void sort(int arr[], int size)

{

       int temp;

       for (int i = 0; i<size; i++)

       {

              for (int j = i + 1; j<size; j++)

              {

                     if (arr[i]>arr[j])

                     {

                           temp = arr[i];

                           arr[i] = arr[j];

                           arr[j] = temp;

                     }

              }

       }

}

int compareOrder(int arr1[], int arr2[], int dsize1, int dsize2)

{     

       if (dsize1 == dsize2)

       {

              if (arr1[0]>arr2[0])

              {

                     return 1;

              }

              else if (arr1[0]<arr2[0])

              {

                     return -1;

              }

              else

              {

                     for (int i = 0; i<dsize1; i++)

                     {

                           if (arr1[i]>arr2[i])

                           {

                                  return 1;

                           }

                           else if (arr1[i]<arr2[i])

                           {

                                  return -1;

                           }                         

                     }                   

                     return 0;

              }

       }

       else

       {

              if (dsize1>dsize2)

              {

                     return 1;

              }

              else

              {

                     return -1;

              }

       }

}

int compareSum(int arr1[], int arr2[], int dsize1, int dsize2)

{

       int sum1 = 0, sum2 = 0;

       for (int i = 0; i<dsize1; i++)

       {

              sum1 += arr1[i];

       }

       for (int i = 0; i<dsize2; i++)

       {

              sum2 += arr2[i];

       }

       if (sum1 == sum2)

              return 0;

       else if (sum1<sum2)

       {

              return -1;

       }

       else if (sum1>sum2)

       {

              return 1;

       }

}

int main()

{

    const int SIZE = 1000;

       string file1 = "Inputfile.txt", file2 = "Inputfile1.txt";

       int dsize1 = 0, dsize2 = 0;

       int Array1[SIZE],Array2[SIZE];

       ReadData(file1, Array1, dsize1);

       ReadData(file2, Array2, dsize2);

       cout << "Arrays before sorting: " << endl;

       printArray(Array1, dsize1);

       printArray(Array2, dsize2);

       cout << "\nArrays after sorting: " << endl;

       sort(Array1, dsize1);

       printArray(Array1, dsize1);

       sort(Array2, dsize2);

       printArray(Array2, dsize2);

       int order = compareOrder(Array1, Array2,dsize1,dsize2);

       int sum = compareSum(Array1, Array2, dsize1, dsize2);

       cout << endl;

       if (order = 0)

       {

              cout << "The two arrays are equal"<<endl;

       }

       else if (order = 1)

       {

              cout << "Array1 is gretaer than Array2" << endl;

       }

       else if (order = -1)

       {

              cout << "Array2 is gretaer than Array1" << endl;

       }

       if (sum = 0)

       {

              cout << "The sum of the two arrays is same" << endl;

       }

       else if (order = 1)

       {

              cout << "Array1 sum is gretaer than Array2" << endl;

       }

       else if (order = -1)

       {

              cout << "Array2 sum is gretaer than Array1" << endl;

       }

      

       return 0;

}

Sample output:

Z

Add a comment
Know the answer?
Add Answer to:
Question 2 In this question, you will read two data files that include integers into two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • Please help code in c++ and use the answers you get from question 1 and 2...

    Please help code in c++ and use the answers you get from question 1 and 2 into the 3rd answer! Question 1 is first, question 2 is in the middle, question 3 is last Input Array (10 pts) te a function only (no main) that takes an array of doubles as a parameter as well as another integer rray. Create a for loop that asks the user to input any real number between-100 and r each element of the array....

  • C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains...

    C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual...

    Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual Studios 2019 as the compiler. This is an individual assignment. You will have lab time to work on this assignment as well as homework time. Each person will analyze two sorts. The first sort will be either bubble sort or insertion sort. The second sort will be either quick sort or merge sort. Create two projects, one for each sort in the given choices....

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT