Question

In: Computer Science

C++ Build a struct with many data members inside (for example, a large array). Design any...

C++

Build a struct with many data members inside (for example, a large array). Design any function that processes the data inside the struct (e.g. adding a few values together and returning the sum.) Write two versions of this function, one that passes by value and one that passes a const reference. Measure the time required to call each function 10,000 times.

Solutions

Expert Solution

C++ code:

#include<iostream>
#include<ctime>
using namespace std;
#define MAX 32000
struct number
{
   int arr[MAX];
};

int sum1(struct number s1)
{
   int total=0;
   for(int i=0;i<MAX;i++)
   {
       total+=s1.arr[i];
   }
   return(total);
}
int sum2(const struct number& s2)
{
   int total=0;
   for(int i=0;i<MAX;i++)
   {
       total+=s2.arr[i];
   }
   return(total);
}
int main()
{
   struct number s;
   const int CLOCKS_PER_MS=CLOCKS_PER_SEC/1000;
   for(int i=0;i<MAX;i++)
   {
       s.arr[i]=i;
   }
   clock_t start1=clock();
   for(int j=0;j<10000;j++)//call using pass by value
   {
       int sm1=sum1(s);  
   }
   clock_t end1=clock();
  
  
   clock_t start2=clock();
   for(int j=0;j<10000;j++)////call using pass by reference
   {
       int sm2=sum2(s);
   }
   clock_t end2=clock();
  
   int time1=(end1-start1)/CLOCKS_PER_MS;//calculate time for pass by value
   int time2=(end2-start2)/CLOCKS_PER_MS;//calculate time for pass by reference
  
   cout<<"Time using pass by value: "<<time1<<endl;
   cout<<"Time using pass by constant reference: "<<time2<<endl;
}

output:


Related Solutions

Create a struct for the following groups of data: (a) Product Data with the following members...
Create a struct for the following groups of data: (a) Product Data with the following members • ID • Name • Price • Quantity (b) Customer Data with the following members • ID • Name • Address • E-mail (c) Sales Data with the following members • Customer ID • Product IDs • Sale amount • Choose the types that you think will work best for the above descriptions. • Names should have a maximum size of 50. • Physical...
c++ Design the Weather class that contains the following members: Data members to store: -a day...
c++ Design the Weather class that contains the following members: Data members to store: -a day (an integer) -a month (an integer) -a year (an integer) -a temperature (a float) -a static data member that stores the total of all temperatures (a float) Member functions: -a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total). -a static...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: -...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: - a day (an integer) - a month (an integer) - a year (an integer) - a temperature (a float) - a static data member that stores the total of all temperatures (a float) Member functions: - a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly...
c++ Create a one-dimension array, y, of this struct data type with a length 4. Pass...
c++ Create a one-dimension array, y, of this struct data type with a length 4. Pass y into a function: PrintArrayElement( …). Finish the interface of this function, and loop over each array element inside this function
In this assignment you will build a small C# project that uses… • A struct •...
In this assignment you will build a small C# project that uses… • A struct • A method with a reference parameter • A while-loop • A switch statement and block instructions: Inside the StringHandler struct add a public void method named Abbreviate. The Abbreviate method must take a string parameter that is passed by reference. This method will take the name of a month (January, February, etc.) as its input and convert it to a 3-letter abbreviation of the...
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
11. Payroll in C++ Design a PayRoll class that has data members for an employee’s hourly...
11. Payroll in C++ Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double. The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that...
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20];...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20]; int age; char country[20]; char Position[20]; double Salary; double Rating; }; First, create an array of BarcelonaPlayer structures. Now, write a function that takes an array of BarcelonaPlayer structures as input and find out the highest paid player among all the players. void highestPaidPlayer(struct BarcelonaPlayer *pl, int size); Create another function that finds all the players from Argentina. void findPlayers(struct BarcelonaPlayer *pl, int size);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT