Need some help with Midterm Assignment

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!

-Shep5158
enter sizes of two 1D arrays (or vectors)

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> makeVec(const size_t vecSize)
{
    std::vector<int> vecFromSize{};
    size_t temp{};
    int input{};
    while(temp < vecSize)
    {
        std::cout << "Enter element " << temp + 1 << " of " << vecSize << ": \n";
        std::cin >> input;
        vecFromSize.push_back(input);
        ++temp;
    }
    return vecFromSize;
}

int main()
{
    std::cout << "Enter size of the first vector: \n";
    size_t vecOneSize{};
    std::cin >> vecOneSize;//input validation for incorrect entry recommended
    std::vector<int> vecOne = makeVec(vecOneSize);

    std::cout << "Enter size of the second vector: \n";
    size_t vecTwoSize{};
    std::cin >> vecTwoSize;
    std::vector<int> vecTwo = makeVec(vecTwoSize);

    vecOneSize < vecTwoSize ? vecOne.resize(vecOne.size() + (vecTwoSize - vecOneSize), 1)
                                : vecTwo.resize(vecTwo.size() + (vecOneSize - vecTwoSize), 1);
    //http://en.cppreference.com/w/cpp/container/vector/resize
    //fills the smaller vector with 1's until it is equal in size to the larger vector
    int* vecOnePtr = &vecOne[0];
    int* vecTwoPtr = &vecTwo[0];

    auto innerProd = std::inner_product(vecOnePtr, vecOnePtr + vecOne.size(), vecTwoPtr, 0);
    //http://en.cppreference.com/w/cpp/algorithm/inner_product
    std::cout << innerProd << "\n";
}
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): error C2593: 'operator <<' is ambiguous

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(488): note: could be 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_streambuf<char,std::char_traits<char>> *)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(468): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(const void *)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(448): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long double)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(428): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(408): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(float)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(388): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned __int64)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(368): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(__int64)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(348): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned long)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(328): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(308): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned int)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(283): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(263): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned short)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(229): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(short)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(209): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(bool)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(203): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(197): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(192): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ostream<char,std::char_traits<char>> &(__cdecl *)(std::basic_ostream<char,std::char_traits<char>> &))'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(1011): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const std::error_code &)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(963): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,unsigned char)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(956): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const unsigned char *)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(949): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,signed char)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(942): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const signed char *)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(816): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(769): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(731): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24911\include\ostream(684): note: or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)'

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
> error C2039: 'inner_product': is not a member of 'std'
> The only direct error on the code is on the "inner_product" on line 43

To use std::inner_product, we #include <numeric>

Try writing this on your own, with two new functions:
one, to print the contents of an array and another, to compute the dot product.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <numeric> // std::inner_product
#include <cassert> // assert
#include <cmath> // std::abs(double)

std::vector<double> make_vec( const char* 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
    const auto 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
        const auto ptr_first_begin = std::addressof( first.front() ) ;
        const auto ptr_second_begin = std::addressof( second.front() ) ;

        // to iterate using pointers, we would also need pointers to the end of the sequence
        const auto ptr_first_end = ptr_first_begin + common_sz ;
        const auto 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
        const auto 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.
I finished my rewrite of the code, and here it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
using namespace std;

vector<double> make_vec(const char* 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
	const auto 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 
		const auto pointer_first_begin = addressof(first.front());
		const auto pointer_second_begin = addressof(second.front());

		const auto pointer_first_end = pointer_first_begin + common_sz;
		const auto 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);

		const auto 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
Last edited on
> 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.
It's fine, then. I'm glad that that you were able to learn/understand what was going on. Well done!
Topic archived. No new replies allowed.