Needs some Help

Cannot fix the identifier problem

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void sum(int[], int[], int[], int);

int main()
{
	const int arraySize = 20;
	int first_array[arraySize] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,20 };
	int second_array[arraySize] = { 11,12,13,14,15,16,17,18,19,20,
		21,22,23,24,25,26,27,28,29,30 };
	int third_array[arraySize] = {};

	cout << setw(7) << "Array 1" << setw(3) << "+" << setw(10)
		<< "Array 2" << setw(3) << "=" << setw(10) << "Array 3" << endl;


	sum(third_array[i] = second_array[i] + first_array[i]);

}

void sum(int a[], int b[], int c[], int sizeArray)
{
	for (int i = 0; i < sizeArray; ++i)
		c[i] = b[i] + a[i];

	cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
		<< b[i] << setw(3) << "=" << setw(10) << c[0] << endl;
}
.
Last edited on
To pass the three arrays to the sum function you need to write their names separated by commas.
 
sum(first_array, second_array, third_array, arraySize);
No matter what I do, I can not resolve the identifier issue in my last 2 lines of code.

cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
<< b[i] << setw(3) << "=" << setw(10) << c[0] << endl;



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
void sum(int[], int[], int[], int);

int main()
{
	const int arraySize = 20;
	int first_array[arraySize] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,20 };
	int second_array[arraySize] = { 11,12,13,14,15,16,17,18,19,20,
		21,22,23,24,25,26,27,28,29,30 };
	int third_array[arraySize] = {};

	cout << setw(7) << "Array 1" << setw(3) << "+" << setw(10)
		<< "Array 2" << setw(3) << "=" << setw(10) << "Array 3" << endl;


	sum(first_array, second_array, third_array, arraySize);
}

void sum(int a[], int b[], int c[], int sizeArray)
{
	for (int i = 0; i < sizeArray; ++i)
		c[i] = b[i] + a[i];

	cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
		<< b[i] << setw(3) << "=" << setw(10) << c[0] << endl;
}


Last edited on
Please post your complete error message. The only problem I see with your last snippet is the lack of #include files and not scoping items from the std namespace.


Looks like you might want those two lines of code to be part of the loop.

1
2
3
4
5
6
for (int i = 0; i < sizeArray; ++i)
{
	c[i] = b[i] + a[i];
	cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
	     << b[i] << setw(3) << "=" << setw(10) << c[i] << endl;
}


closed account (E0p9LyTq)
Look real close at your sum() function. The for loop.

From your second code extract:

Without enclosing lines 21-24 in brackets the for loop only executes line 21 repeatedly. Lines 23-24 are outside the for loop block and the loop variable i is out of scope.

Also, when outputting the arrays it should be c[i], not c[0].

18
19
20
21
22
23
24
25
26
27
void sum(int a[ ], int b[ ], int c[ ], int sizeArray)
{
   for (int i = 0; i < sizeArray; ++i)
   {
      c[i] = b[i] + a[i];

   std::cout << std::setw(7) << a[i] << std::setw(3) << "+" << std::setw(10)
      << b[i] << std::setw(3) << "=" << std::setw(10) << c[i] << '\n';
   }
}

closed account (E0p9LyTq)
An easier way to fill your first and second arrays is the C++ library function std::iota, if the values are sequentially incremented.
http://www.cplusplus.com/reference/numeric/iota/

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
#include <iostream>
#include <iomanip>
#include <numeric>  // std::iota

void sum(int[ ], int[ ], int[ ], int);

int main()
{
   const int arraySize = 20;

   int first_array[arraySize];
   std::iota(first_array, first_array + arraySize, 1);

   int second_array[arraySize];
   std::iota(second_array, second_array + arraySize, 11);

   int third_array[arraySize] { 0 };

   std::cout << std::setw(7) << "Array 1" << std::setw(3) << "+" << std::setw(10)
      << "Array 2" << std::setw(3) << "=" << std::setw(10) << "Array 3" << '\n';

   sum(first_array, second_array, third_array, arraySize);
}

void sum(int a[ ], int b[ ], int c[ ], int sizeArray)
{
   for (int i = 0; i < sizeArray; ++i)
   {
      c[i] = b[i] + a[i];

      std::cout << std::setw(7) << a[i] << std::setw(3) << "+" << std::setw(10)
         << b[i] << std::setw(3) << "=" << std::setw(10) << c[i] << '\n';
   }
}
std::iota is a very useful function, but why such a meaningless name?
Thomas1965 wrote:
std::iota is a very useful function, but why such a meaningless name?

"iota" means a very small amount. The function uses ++ to move from one value to the next, so it is incrementing by the smallest amount.
Last edited on
closed account (E0p9LyTq)
std::iota is a very useful function, but why such a meaningless name?

https://stackoverflow.com/questions/9244879/what-does-iota-of-stdiota-stand-for
Thanks @tpb, @FurryGuy
So again I have tried a few different approaches, no luck!
Here is the actual error that I am getting

1>c:\users\bruce williams\documents\c++\written exercise 2\written exercise 2\written exercise 2.cpp(33): error C2065: 'i': undeclared identifier
1>c:\users\bruce williams\documents\c++\written exercise 2\written exercise 2\written exercise 2.cpp(34): error C2065: 'i': undeclared identifier
1>Done building project "Written Exercise 2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void sum(int[], int[], int[], int);

int main()
{
	const int arraySize = 20;
	int first_array[arraySize] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,18,20 };
	int second_array[arraySize] = { 11,12,13,14,15,16,17,18,19,20,
		21,22,23,24,25,26,27,28,29,30 };
	int third_array[arraySize] = {};

	cout << setw(7) << "Array 1" << setw(3) << "+" << setw(10)
		<< "Array 2" << setw(3) << "=" << setw(10) << "Array 3" << endl;


	sum(first_array, second_array, third_array, arraySize);
}

void sum(int a[], int b[], int c[], int sizeArray)
{
	for (int i = 0; i < sizeArray; ++i)
		c[i] = b[i] + a[i];

	cout << setw(7) << a[i] << setw(3) << "+" << setw(10)
		<< b[i] << setw(3) << "=" << setw(10) << c[i] << '\n';
}
closed account (E0p9LyTq)
Look at your sum() function, as I earlier said.

Lines 30 and 31 are OUTSIDE your for loop, i is no longer in scope when the for loop ends.

27
28
for (int i = 0; i < sizeArray; ++i)
   c[i] = b[i] + a[i];

is the same as

27
28
29
30
for (int i = 0; i < sizeArray; ++i)
{
   c[i] = b[i] + a[i];
}

Your function needs to be written as follows

27
28
29
30
31
32
33
34
35
36
void sum(int a[ ], int b[ ], int c[ ], int sizeArray)
{
   for (int i = 0; i < sizeArray; ++i)
   {
      c[i] = b[i] + a[i];

   std::cout << std::setw(7) << a[i] << std::setw(3) << "+" << std::setw(10)
      << b[i] << std::setw(3) << "=" << std::setw(10) << c[i] << '\n';
   }
}

You MUST enclose both statements within parentheses to work properly.

Learning to use parentheses with even single statement lines makes it easier to "catch" simple errors like the one you are getting.

Thank you!!!!!! That was it, I couldn't see it. I am a newby when it comes to this.
Topic archived. No new replies allowed.