Hi,
I am currently working on one of the three assignments for my midterm project and I am having a difficult time using pointer as my professor is asking in this assignment. To make it easy this is the assignment we were given.
"Prompt the user to enter sizes of two 1D arrays (or vectors) and then input their elements. If user enters different sizes then expand the smaller array to include extra elements initialized as 1. Define two pointers and point them to each input array. Do not use any subscripts within any loops; use pointers only. Now perform the dot product and print the result. Note that user may enter positive or negative values for arrays. In a user-friendly manner, print original input arrays, their original sizes, and the result of dot product."
Now what I have so far may be completely wrong, which is why I need some assistance. I started writing the code for this and I got to a point were I simply didn't know what to do next. The code will be below.
#include<iostream>
using namespace std;
int ArraySize()
{
int SIZE;
cout << "Please enter the size of the arrays:\n";
cin >> SIZE;
return SIZE;
}
int main()
{
int size;
size = ArraySize();
int *arrayOne[1];
int *arrayTwo[1];
delete[] arrayOne;
int *arrayOne = new int[size];
delete[] arrayTwo;
int *arrayTwo = new int[size];
cout << "Please enter the values for array 1:\n";
cin >> *arrayOne[size];
cout << "Please enter the values for array 2:\n";
cin >> *arrayTwo[size];
return 0;
}
void ProductArray(int arrayProduct)
{
}
I planned on using the empty function at the bottom but I was unsure of how I was going to use the array he wanted with the pointers. Any help is appreciated!
Your professor has (quite remarkably!)) given you the option to use vectors, grab it and this'd allow you to use:
(a) the resize() member method of std::vector to quickly fill the smaller vector with 1's until it is equal in size to the larger vector, and,
(b) the algorithm std::inner_product to calculate the inner/dot product of the two vectors. For this algorithm additional pointer variables are not strictly required as we could have used some of the iterators that come with std::vector but, to address the assignment's requirements, we'll be using pointers below:
So should I just scrap my code and work off of this now? And I ran it through and I got a bunch of errors that I will post below. My big issue with this is that we haven't covered vectors in the book yet and I think he allowed to use of them for the people that happened to know how to use them. And on top of that I am not very good with pointers past the basic uses as I said at the top so when they start getting used in more complex ways (Which I know is how they are used a lot of times) I start to get lost. Let me know your thoughts
There are a lot of errors so I had to post them separate.
1>c:\users\brayden\desktop\spring semster 2017\c++\week 11\midterm 2\midterm 2\parraydotproduct.cpp(43): error C2039: 'inner_product': is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\algorithm(14): note: see declaration of 'std'
1>c:\users\brayden\desktop\spring semster 2017\c++\week 11\midterm 2\midterm 2\parraydotproduct.cpp(43): error C3861: 'inner_product': identifier not found
1>c:\users\brayden\desktop\spring semster 2017\c++\week 11\midterm 2\midterm 2\parraydotproduct.cpp(44): note: while trying to match the argument list '(std::ostream, unknown-type)'
1>c:\users\brayden\desktop\spring semster 2017\c++\week 11\midterm 2\midterm 2\parraydotproduct.cpp(26): fatal error C1075: the left brace '{' was unmatched at the end of the file
1>Done building project "Midterm 2.vcxproj" -- FAILED.
I am sure a lot of there are errors that have easy fixes and are linked together but as being new to the program I have a hard time following where the issues are and how to fix them. The only direct error on the code is on the "inner_product" on line 43
#include <iostream>
#include <vector>
#include <numeric> // std::inner_product
#include <cassert> // assert
#include <cmath> // std::abs(double)
std::vector<double> make_vec( constchar* vec_name )
{
std::cout << "enter size of " << vec_name << " vector: " ;
std::size_t sz = 0 ;
std::cin >> sz ;
std::vector<double> vec(sz) ;
if( sz > 0 ) std::cout << "enter " << sz << " numbers\n" ;
for( double& v : vec ) std::cin >> v ;
return vec ;
}
int main()
{
auto first = make_vec( "first" ) ;
auto second = make_vec( "second" ) ;
// If user enters different sizes then expand the smaller array
// to include extra elements initialized as 1
constauto common_sz = std::max( first.size(), second.size() ) ;
first.resize( common_sz, 1.0 ) ;
second.resize( common_sz, 1.0 ) ;
if( common_sz > 0 ) // if the vectors are not empty
{
// Define two pointers and point them to each input array
// Your teacher appears to have used incorrect terminology
// This is the intent:
// define two pointers and point them to the first element of each input array
constauto ptr_first_begin = std::addressof( first.front() ) ;
constauto ptr_second_begin = std::addressof( second.front() ) ;
// to iterate using pointers, we would also need pointers to the end of the sequence
constauto ptr_first_end = ptr_first_begin + common_sz ;
constauto ptr_second_end = ptr_second_begin + common_sz ;
// take it up from here
// for instance, to print the contents of 'first' using these pointers
// "Do not use any subscripts within any loops; use pointers only"
for( auto ptr = ptr_first_begin ; ptr != ptr_first_end ; ++ptr )
std::cout << *ptr << ' ' ;
std::cout << " (size==" << common_sz << ")\n" ;
// compute the dot product using home-grown code
// "Do not use any subscripts within any loops; use pointers only"
double dot_product = 0.0 ;
auto ptr1 = ptr_first_begin ;
auto ptr2 = ptr_second_begin ;
while( ptr1 != ptr_first_end ) dot_product += *ptr1++ * *ptr2++ ;
assert( ptr1 == ptr_first_end && ptr2 == ptr_second_end ) ; // sanity check
// compute the dot product using algorithm std::inner_product
constauto inner_product = std::inner_product( ptr_first_begin, ptr_first_end, ptr_second_begin, 0.0 ) ;
assert( std::abs( dot_product - inner_product ) < 0.0000001 ) ; // verify that the two are equal
std::cout << dot_product << ' ' << inner_product << '\n' ;
}
}
Thanks for breaking it down and explaining, I am starting to understand what I need to be doing. I am going to write my own version of this now. I will post again with the results. Good or bad. Thanks for the help thus far.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
usingnamespace std;
vector<double> make_vec(constchar* vec_name)
{
cout << "Enter the size of " << vec_name << " vector: ";
size_t sz = 0;
cin >> sz;
vector<double> vec(sz);
if (sz > 0)
cout << "Enter " << sz << " numbers\n";
for (double& v : vec)
cin >> v;
return vec;
}
int main()
{
auto first = make_vec("First");
auto second = make_vec("Second");
// Expand smaller array and make the exrta elements 1
constauto common_sz = max(first.size(), second.size());
first.resize(common_sz, 1.0);
second.resize(common_sz, 1.0);
if (common_sz > 0)
{
// Defining pointers and using them
constauto pointer_first_begin = addressof(first.front());
constauto pointer_second_begin = addressof(second.front());
constauto pointer_first_end = pointer_first_begin + common_sz;
constauto pointer_second_end = pointer_second_begin + common_sz;
cout << "The first array is";
for (auto pointerA = pointer_first_begin; pointerA != pointer_first_end; ++pointerA)
cout << " " << *pointerA;
cout << "\nThe second array is";
for (auto pointerB = pointer_second_begin; pointerB != pointer_second_end; ++pointerB)
cout << " " << *pointerB;
cout << "\nThe size is " << common_sz << endl;
// computing the dot product
double dot_product = 0.0;
auto pointer1 = pointer_first_begin;
auto pointer2 = pointer_second_begin;
while (pointer1 != pointer_first_end)
dot_product += *pointer1++ * *pointer2++;
// sanity check
assert(pointer1 == pointer_first_end && pointer2 == pointer_second_end);
constauto inner_product = std::inner_product(pointer_first_begin, pointer_first_end, pointer_second_begin, 0.0);
// Making use that the two are equal
assert(abs(dot_product - inner_product) < 0.000001);
cout << "The dot product is " << dot_product << endl;
}
return 0;
}
It works exactly how I need it to work! Thanks for the help, I probably would have ended up with a terrible grade on that part. You are truly a lifesaver. Have a good rest of your night if you are still up/ day if you live elsewhere in the world.
Final results:
Enter the size of First vector: 8
Enter 8 numbers
1
1
3
6
9
7
5
2
Enter the size of Second vector: 5
Enter 5 numbers
3
1
7
4
9
The first array is 1 1 3 6 9 7 5 2
The second array is 3 1 7 4 9 1 1 1
The size is 8
The dot product is 144
> I finished my rewrite of the code, and here it is.
You haven't really done a rewrite, have you?
Other than also printing out the second array, and changing the names of some variables.
You would have learnt something useful, and experience the consequential sense of personal fulfilment, if you are able to start with a clean slate, and then write this program on your own - ie. without copying and editing lines from existing code.
I did go through line by line and rewrite the code, a used a lot of the same names just because it helped me follow what was going on in the program. I mean yes I did use what you posted as a basis because I had no idea of how to write it if I didn't. But rewriting it did help me understand what was actually going on with the vectors and how the pointers were used to store the array and spit it back out. I didnt just copy your code, paste it, and edit is slightly. Again I actually took the time to write the code out from a blank file so I'm sorry if it seemed like I just used what you posted without trying to learn what was going on.