Question

in c++ 1)Code the function definition for twoFunction, picking up the array myscore. myscore has no...

in c++

1)Code the function definition for twoFunction, picking up the array myscore. myscore has no return value.

2)How can the function twoFunction change the contents of the second element of myscore?

3) If twoFunction does change the contents of the second element of myscore, what happens to the second element of myscore in the calling program?

Homework Answers

Answer #1

#include <iostream>
using namespace std;

void twoFunction(int myscore[]) // 1)
{
   myscore[1] = 100; // 2) the second element of the array is changed
}

int main() {
   int myscore[] = {1,2,3,4,5};
  
   cout<<"\nArray before function call : ";
   for(int i=0;i<5;i++)
cout<<myscore[i]<<" ";
  
twoFunction(myscore);
  
cout<<"\nArray after function call : ";
for(int i=0;i<5;i++)
cout<<myscore[i]<<" "; // 3) After function call , the value of second element of array is changed.
  
   return 0;
}

Output:

Array before function call : 1 2 3 4 5 
Array after function call : 1 100 3 4 5 

2)

The value of the second element of array changes after calling the function. This is due to the fact that array name which is passed as argument to the function will act as the base address of array. Any changes made into the array will be done on addresses. That's why the change is reflected back in the main function.

Do ask if any doubt. Please upvote.

Know the answer?
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
Similar Questions
C++ 1a) Assume that you have an integer variable named myint. Call xyzfunc and pass myint...
C++ 1a) Assume that you have an integer variable named myint. Call xyzfunc and pass myint as a parameter. a) xyzfunc(myint) b) myint(xyzfunc) c) xyzfunc(&myint) c) call xyzfunc(myint) 1b) Code the function definition for xyzfunc, picking up a local copy of myint. xyzfunc has no return value. a) void xyzfunc (int &myint); b) void xyzfunc (myint); c) void xyzfunc (local myint); d) void xyzfunc (int myint); 1c) If xyzfunc executes myint++, what happens to the copy of myint in the...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return the number of positive and negative in the array through the use of parameters. In addition, it also returns the number of 0s in the array as the return value. For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4 for positives, 2 for negatives and 1 for 0's as the return value. 2. Write a function named "rotateRight" that...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
• First, create a function called addNumber, which has a formal parameter for an array of...
• First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
C++ visual studios this function has to run twice. and the "count" variable needs to be...
C++ visual studios this function has to run twice. and the "count" variable needs to be "2" on the second run. How can i do that? The parameters can not change as well void printArray(const int *array, int n) {    int count = 1;    char check;    if (check == 'k')    {        count++;    }    cout << "Value " << count << " array contents." << endl;    cout << "------------------------" << endl;   ...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
(C++) Write a function called triple that takes an array of integers as a parameter as...
(C++) Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
C++ 5a)We have the following class: class Banana { private:      string s;      int y;...
C++ 5a)We have the following class: class Banana { private:      string s;      int y; public: bool theMemberFunc (string); void setterForS(string); // setter for s string getterForS(); // getter for s }; Instantiate a static object of Banana named co. a)int Banana; b)co.Banana = new; c)Banana = new class; d)Banana co; 5b)Code a call to function aNonclassFunction passing co. a)aNonclassFunction (co); b)aNonclassFunction (* co); c)aNonclassFunction (aGoodClass.co); d)aNonclassFunction (aGoodClass); 5c)Code the function definition for aNonclassFunction, picking up co. aNonclassFunction has...
The code segment in Listing 1, when completed and executed, creates nums, an uninitialized 4-integer array....
The code segment in Listing 1, when completed and executed, creates nums, an uninitialized 4-integer array. It stores 4 and 5 as the first and second elements, respectively, of the array. It adds the first and second elements of the array and stores their sum as the third element of the nums. It prints the third element of nums. It then adds the second and third elements of the array and stores their sum as the fourth element of nums....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT