Question

c++ please no global varaible write a value returning function input data to dynamically allocate a...

c++ please no global varaible

write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7 8 9 10. The data in the array beginning with element 0 would be 10 9 8 7 6 5 4 3 2 `1.

All functions must use pointer to access array and pointer must be used terminate loops.You must increment a pointer to access successive elements of array. No INTEGER CONTROLLED LOOPS AND SUBSCRIPTS SUCH AS array[n] or *(array+n) and no integer counter.

#include<fstream>

#include<iostream>

#include<stdlib.h>

#include<new>

using namespace std;

void display_data(short *data, short size);         // function to display data in an array

short * input_data(short size, ifstream &filein);   // allocate memory and read the data from the file and store it in the array in reverse order

short compare_arrays(short *list1, short size1, short *list2, short size2); // compare two arrays

short palindrome(short *data, short size);                  // examine the contents of the array to determine if the data is a palindrome

void GreaterThanAvg(short *data, short &size, short &avg); // find all values in the array which are greater than the integer average of all the data in the array

int main()

{   

    short *list1, size1,   // pointer to list1 array and size1    

              *list2, size2,   // pointer to list1 array and size1  

               average,         // average value in the array         

                results;           // used to store the result returned from a function

    short setNO=1;          // variable to keep track of the number of data sets entered

        

    //cout<<"Program will store data in the array\n";

    //cout<<"Program will compare two arrays to determine if they are identical, in reverse order of each other or not the same\n";

    //cout<<"Program will also determine if the data in an array is a palindrome\n";  

   

    ifstream filein("FinalExam.txt"); // enter the size of the array from the disk firl

  

    while(filein>>size1) // loop to permit user to test many data sets

    {      

        cout<<"----------------------DataSet "<<setNO++<<"-----------------------\n";

        list1 = input_data(size1, filein);                 

        cout<<"\nthere are "<<size1<<" values in the list1 array\n";      

        display_data(list1, size1);   

        filein>>size2;   // enter the size of the second array

        list2 = input_data(size2, filein);         

        cout<<"there are "<<size2<<" values in the list2 array\n";       

        display_data(list2, size2);  

        palindrome(list1, size1)? cout<<"list1 was a palindrome\n" : cout<<"list1 was not a palindrome\n"; // this is ternary test to for palindrome

        palindrome(list2, size2)? cout<<"list2 was a palindrome\n" : cout<<"list2 was not a palindrome\n";

        results = compare_arrays(list1, size1, list2, size2); // compare two arrays

        if(results == 1)

             cout<<"List1 and List2 were identical arrays\n";

        else if(results == 2)            

             cout<<"List1 and List2 were in reverse order of each other\n";

        else if(results == 3)            

             cout<<"List1 and List2 are different arrays\n";

        else

             cout<<"an incorrect value was returned from the function - probably a logic error\n";   

        GreaterThanAvg(list1, size1, average); // function to find all numbers in the array greater than the average

             cout<<"There were "<<size1<<" values in list1 that were greater than the average value of "<<average<<endl;

        if(size1) display_data(list1, size1);

        GreaterThanAvg(list2, size2, average);

        cout<<"There were "<<size2<<" values in list1 that were greater than the average value of "<<average<<endl;

        if(size2) display_data(list2, size2);                            

        cout<<endl;           

        delete [] list1; // delete the memory allocated

        delete [] list2;                                        

    }   

    // pause the program to see the results

     //system("pause");

     return 0;      

}

void display_data(short *data, short size)

{

    short i;

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

    { // display the numbers in the array separated by 1 blank all on the same line          

      cout<<data[i]<<' ';

    }

    cout<<endl;    

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<fstream>
#include<iostream>
#include<stdlib.h>

using namespace std;

void display_data(short *data, short size);         // function to display data in an array

short * input_data(short size, ifstream &filein);   // allocate memory and read the data from the file and store it in the array in reverse order

short compare_arrays(short *list1, short size1, short *list2, short size2); // compare two arrays

short palindrome(short *data, short size);                  // examine the contents of the array to determine if the data is a palindrome

void GreaterThanAvg(short *data, short &size, short &avg); // find all values in the array which are greater than the integer average of all the data in the array

int main()

{   

        short *list1, size1,   // pointer to list1 array and size1    

        *list2, size2,   // pointer to list1 array and size1  

        average,         // average value in the array         

        results;           // used to store the result returned from a function

        short setNO=1;          // variable to keep track of the number of data sets entered



    //cout<<"Program will store data in the array\n";

    //cout<<"Program will compare two arrays to determine if they are identical, in reverse order of each other or not the same\n";

    //cout<<"Program will also determine if the data in an array is a palindrome\n";  



    ifstream filein("FinalExam.txt"); // enter the size of the array from the disk firl



    while(filein>>size1) // loop to permit user to test many data sets

    {      

        cout<<"----------------------DataSet "<<setNO++<<"-----------------------\n";

        list1 = input_data(size1, filein);                 

        cout<<"\nthere are "<<size1<<" values in the list1 array\n";      

        display_data(list1, size1);   

        filein>>size2;   // enter the size of the second array

        list2 = input_data(size2, filein);         

        cout<<"there are "<<size2<<" values in the list2 array\n";       

        display_data(list2, size2);  

        palindrome(list1, size1)? cout<<"list1 was a palindrome\n" : cout<<"list1 was not a palindrome\n"; // this is ternary test to for palindrome

        palindrome(list2, size2)? cout<<"list2 was a palindrome\n" : cout<<"list2 was not a palindrome\n";

        results = compare_arrays(list1, size1, list2, size2); // compare two arrays

        if(results == 1)

             cout<<"List1 and List2 were identical arrays\n";

        else if(results == 2)            

             cout<<"List1 and List2 were in reverse order of each other\n";

        else if(results == 3)            

             cout<<"List1 and List2 are different arrays\n";

        else

             cout<<"an incorrect value was returned from the function - probably a logic error\n";   

        GreaterThanAvg(list1, size1, average); // function to find all numbers in the array greater than the average

             cout<<"There were "<<size1<<" values in list1 that were greater than the average value of "<<average<<endl;

        if(size1) display_data(list1, size1);

        GreaterThanAvg(list2, size2, average);

        cout<<"There were "<<size2<<" values in list1 that were greater than the average value of "<<average<<endl;

        if(size2) display_data(list2, size2);                            

        cout<<endl;           

        delete [] list1; // delete the memory allocated

        delete [] list2;                                        

    }   

    // pause the program to see the results

     //system("pause");

     return 0;      

}

void display_data(short *data, short size)

{

    short i;

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

    { // display the numbers in the array separated by 1 blank all on the same line          

      cout<<data[i]<<' ';

    }

    cout<<endl;    

}


   // allocate memory and read the data from the file and store it in the array in reverse order
short * input_data(short size, ifstream &filein) {
        filein >> size;

        short *data = new short[size];
        short *end = data + size + 1;

        do {
                end -= 1;
                filein >> *end;
        } while(end != data);

        return data;
}

short compare_arrays(short *list1, short size1, short *list2, short size2) {
        if(size1 != size2) {
                return 3;
        }
        short *end1 = list1 + size1;
        short *end2 = list2 + size2;

        short *p1 = list1;
        short *p2 = list2;

        bool same = true;
        while((p1 != end1) && (p2 != end2)) {
                if(*p1 != *p2) {
                        same = false;
                        break;
                }
                p1 += 1;
                p2 += 1;
        }

        if(same) {
                return 1;
        }

        same = true;
        p2 = end2 - 1;
        while((p1 != end1) && (p2 != list2-1)) {
                if(*p1 != *p2) {
                        same = false;
                        break;
                }
                p1 += 1;
                p2 -= 1;
        }

        if(same) {
                return 2;
        }

        return 3;
}

short palindrome(short *data, short size) {
        short *end = data + size;
        short *originalStart = data;

        do {
                end = end-1;

                if(*data != *end) {
                        return 0;
                }
                data += 1;
        }while(end != originalStart);
        return 1;
}

// find all values in the array which are greater than the integer average of all the data in the array
void GreaterThanAvg(short *data, short &size, short &avg) {
        short *end = data + size;
        short *originalStart = data;

        short sum = 0;
        short count = size;

        do {
                end = end-1;
                sum += *end;
        }while(end != originalStart);
        avg = sum/count;

        end = data + size;
        size = 0;
        do {
                end = end-1;
                if(*end > avg) {
                     size++;   
                }
        }while(end != originalStart);
}

I have filled in the code as much as i can, but i dont have your input file.. so could not do a test run.. Please check and let me know if any issues, Thanks!

Add a comment
Know the answer?
Add Answer to:
c++ please no global varaible write a value returning function input data to dynamically allocate a...
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
  •      program will enter data into two single dimension arrays (do not st...

         program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible;...

    C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible; (2) use unique value array; (3) use flag array (flags used to avoid counting a number more than 1 time); (4) compute frequency distribution (use unique and count arrays); (5) use count array to store the frequency of a number. No global variable are permitted in this assignment. Do not change the code provided only write the missing code. Write the missing C++ statements...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

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
ADVERTISEMENT