Hi!, having trouble with this one,
In this class we use visual studio, C++ language
--------------------------------------------------------------
Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions)
Part 1 - Using Pointers
int largeArray (const int [], int);
int largePointer(const int * , int);
void fillArray (int * , int howMany);
void printArray (const char *,ostream &, const int *, int
howMany);
const int low = 50; const int high = 90;
void main()
{
const int MAX_SIZE = 40;
int info[MAX_SIZE];
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 || used > MAX_SIZE )
{ cout << "Enter a value between 1 and " << MAX_SIZE
<< "How many elements in the array? ";
cin >> used;
}
fillArray (info,used);
printArray("Array of data",cout,info,used);
cout << "The largest element using subscripts is "
<< largeArray (info,used) << endl;
cout << "The largest element using pointers is " <<
largePointer(info,used) << endl; }
//ENDMAIN
void printArray(const char * m,ostream & Out,const int * p,
int hm)
{
Out << m << endl;
for(int i = 0; i < hm; i++)
{
Out << p[i] << endl;
}
}
void fillArray(int * p , int howMany) // as parameters, pointers
and arrays are one and the same
{ int range = high - low + 1;
srand(time(0));
for( int i = 0; i < howMany; i++)
{
p[i] = rand() % range + low;
}
}
int largeArray(const int data[], int howMany) // use
subscripts
{
int largest = 0;
// 1 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE SUBSCRIPTS
TO ACCESS THE ELEMENTS
return largest;
}
int largePointer(const int * data, int howMany) // use
pointers
{
int largest = 0;
// 2 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE POINTERS TO
ACCESS THE ELEMENTS
return largest;
}
----------------HINT FOR PART 1--------------------------------------
PART 1 - HINTS AND COMMENTS BELOW...PAY PARTICULAR ATTENTION TO THE IDEA OF ADDRESSES VS POINTERS
int largeArray (const int [], int);
int largePointer(const int * , int);
The above function prototypes both find the largest item in an
integer array
The first prototye passes the integer array, but (like all arrays)
passes the address of the first element of the array
The second prototype passes a pointer to the first element of the
array
...so essentially they are doing the exact same thing in two
different ways...ie address vs pointer
...the second int parameter is, of course, the size of the
array
void fillArray (int * , int howMany);
The above function prototype (which will load an array with integers) passes a pointer to the array to be filled
void printArray (const char *,ostream &, const int *, int howMany);
The above function prototype will print the contents of an
array.
The first parameter is a character pointer, pointer to a string of
chars which will be output as a heading
The second parameter is the output stream (cout, in this example)
where the output will go
The third parameter is an integer pointer, pointing to an array of
integers
The fourth parameter is the size of the array to be printed
const int low = 50; const int high = 90; ...used to randomly select integers in the range of 50- 90
void main()
{
const int MAX_SIZE = 40; ...this forces a limit on the number of
integers that we can store in an array
int info[MAX_SIZE];
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 || used > MAX_SIZE ) ...this while forces
the user to input a number between 1 and 40
{
cout << "Enter a value between 1 and " << MAX_SIZE
<< "How many elements in the array? ";
cin >> used;
}
fillArray (info,used); ...NOTICE HERE ...the address of array
info is passed
printArray("Array of data",cout,info,used); ...NOTICE HERE ...the
address of the string and int * pointer to "info" is passed
...THE ONLY WAY YOU CAN TELL IS BY LOOKING AT THE FUNCTION
PROTOTYPES AT THE TOP OF THE PROGRAM!
cout << "The largest element using subscripts is "
<< largeArray (info,used) << endl;
cout << "The largest element using pointers is " <<
largePointer(info,used) << endl; }
In the above two CALLS TO the functions (largeArray and
largePointer)
...array "info" is passed as an address in the first function
call
...array "info" is passed as a pointer in the second function
call
The only way you can tell is by looking at the prototypes at the
top of this sample!!
//ENDMAIN
void printArray(const char * m,ostream & Out,const int * p,
int hm)
{
...notice m is a character pointer,now pointing to the string
"Array of data" which is a array of chars
Out << m << endl;
...Out is an ostream...passed as "cout"
for(int i = 0; i < hm; i++) ..."hm" is the howmany alias...ie
size of the array
{
Out << p[i] << endl;
... p is an "int *"..ie a pointer to an integer...BUT LIKE ALL
POINTERS CAN BE USED AS IF IT WERE AN ARRAY
}
}
void fillArray(int * p , int howMany) // as parameters, pointers
and arrays are one and the same
{ int range = high - low + 1;
srand(time(0));
for( int i = 0; i < howMany; i++)
{
p[i] = rand() % range + low;
...p, again, is passed as an "int *" pointer...BUT LIKE ALL
POINTERS CAN BE USED AS IF IT WERE AN ARRAY
}
}
int largeArray(const int data[], int howMany) // use
subscripts
{
int largest = 0;
// 1 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE SUBSCRIPTS
TO ACCESS THE ELEMENTS
...USE A FOR LOOP AND DATA[i] references, like in the fillArray
functions
...TO STORE DATA[i] IN LARGEST
return largest;
}
int largePointer(const int * data, int howMany) // use
pointers
{
int largest = 0;
// 2 - WRITE THE CODE TO FIND THE LARGEST NUMBER - USE POINTERS TO
ACCESS THE ELEMENTS
...USE A FOR LOOP AND "*DATA" references instead of data[i]
references
...TO STORE *DATA IN LARGEST
return largest;
}
---------------------------------------
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...
C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...
SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
C++, Will Upvote: A) Try to get the following programs to run in your environment. B) If the programs will run, document each line of code with comments and describe any changes you had to make to the original code to get it to work. If the programs are unable to be modified to run with desirable results, explain why you think so. C) Description your solution and a synopsis of how your code is intended to work and the...
Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T> T data TreeNode<T> left TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...
Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...