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...
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...
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 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);
C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY. I have an array of NODES, NODE nodes[513];...
C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY. I have an array of NODES, NODE nodes[513]; typedef struct node { int weight;    } NODE; I have a value called int num_nodes which gives the number of positions in that array which are filled. What I need to do: there is a value inside of nodes[0], but there are other values in the node array which can be found at index nodes[256] to nodes[256+num_nodes]. I need to shift all of...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT