sQuestion about finding specific Content in an Array

I have an array of 20 integers and one of the questions is to find the locations and content values if any, where the content of the array is three times the index value. I know that there are 4 values in the array that are 3 times their index value. For example a[0] is 3, a[3] is 9, a[8] is 24, and a[12] is 36. The code for everything is below with comments, but its not giving me what I want. It is only printing out the first element with with a value 3 times that element (i.e. a[0] and it value of 3. Could someone please explain to me what I am doing wrong. Would I need to do a linear search? Any assistance would be appreciated, thanks.

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
void part7(const int[], int);//function prototype
const int arraySize = 20;

//initialize array
int a[arraySize] = {3, 55,2,9,35,36,1,212,24,311,7,101,26,42,555,52,17,18,57,554};

part7(a, arraySize);//function call

void part7(const int b[], int size)//function definition
{
	cout << endl;
	

	cout << setw(20) << "Part VII " << endl << endl;
	
		
	cout << "Element " << setw(12) << "Value " << endl;
	
		
	for(int i = 0; i < size; i++)
	{
	  if(b[i] == b[i*3])
			
	cout << setw(6) << i << setw(12) << b[i] << endl;
	
	}
	
}

First of all, a[0] should not be a value. Think about it: 3 * 0 = 0, not 3.

Take a closer look at the if statement you used:

if(b[i] == b[i*3])

The flaw in this can be seen easiest with a simple case study: When i = 0, it says:

if(b[0]==b[0]) //This will always be true.

However, when i=3 for example:

1
2
3
if(b[3]==b[9]) //This is actually comparing the value at index 3 to the value at index 9.
//Broken down even further:
if(9==311) //Obviously not. 


Here's how to do what you described above:

if(b[i]==3*i)

I hope this helps.
Yes that helped and gave me what I was looking for. Thanks for the explanation as well. One more thing to clarify you said that a[0] should not be a value. But the value in that first position of a[0] is 3 which is three times that of the index i.e 1. In my output it does not give me that value, It only gives me indexes 3, 8 and 12 with their respective values of 9, 24, and 36. How come it didn't give me index 1 with its value of 3? Sorry if I'm missing the concept here I'm just trying to understand it more clearly.
The concept you're missing is this: Arrays values are indexed starting at 0. So if an array is declared to have 20 elements as such:

int a[20];

Then valid entries in that array are: a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], a[16], a[17], a[18], and a[19]. Trying to assign a value to a[20] (or access one there) would be serious error that will produce unpredictable results. To illustrate the point, try compiling and running this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    int a[5]={6, 4, 17, 13, 8};
    cout<<"Valid parts of the array:"<<endl;
    cout<<"a[0] = "<<a[0]<<endl;
    cout<<"a[1] = "<<a[1]<<endl;
    cout<<"a[2] = "<<a[2]<<endl;
    cout<<"a[3] = "<<a[3]<<endl;
    cout<<"a[4] = "<<a[4]<<endl;
    
    cout<<"\n\n";
    cout<<"Here's what happens when you try to access a[5]:"<<a[5]<<endl;
    system ("pause");
    
}


Notice how a[5] came up with something that wasn't entered. That's because a[5] doesn't actually exist as part of the array. The computer is accessing the point of memory just outside the array. (Where the next value would have been if the array were larger.)

VERY IMPORTANT TO REMEMBER: The compiler will not stop you from writing a program to both assign and read from values outside the range of the array. However this is very bad, because that memory is not reserved for the array. What that means is that you may end up acessing into another part of memory that was reserved for something else. That can have serious repercussions. DON'T DO IT!

Hope this helps.

Topic archived. No new replies allowed.