Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please tell me why I get the error if indexes gets out of range.My error is once

ID: 3747073 • Letter: P

Question

Please tell me why I get the error if indexes gets out of range.My error is once out-of-range index array stores a certain value, it automatically assign that value for every other out of range index array as well.

Here is my code, please tell me where to fix it.

Header 'h' file code:

#ifndef StaticArray_h
#define StaticArray_h

template <typename V, int CAP>
class StaticArray
{
V values[CAP];
V dummy;


public:
StaticArray();
int capacity( ) const {return CAP;};
V operator[](int) const;
V& operator[](int);
};

template <typename V, int CAP>
StaticArray<V, CAP>::StaticArray()
{
for (int i=0; i < CAP; i++)
values[i] = V();

dummy = V();
};

template <typename V, int CAP>
V& StaticArray<V, CAP>::operator[ ](int index)
{
if (index < 0) return dummy;
if (index >= CAP) return dummy;
return values[index];
}

template <typename V, int CAP>
V StaticArray<V, CAP>::operator[ ](int index) const
{
if (index < 0) return 0;
if (index >= CAP) return 0;
return values[index];
}


#endif

CPP code:

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

#include <cassert>

#include "StaticArray.h"

#include "StaticArray.h" //ifndef test

int main( )

{

StaticArray<double, 100> a;

StaticArray<double, 100> b;

int indexcount = 0;

int i;

double d;

char index[10];

char data[10];

char search[10];

do

{

cout << "Input an index and a value [Q to quit]: " << endl;

cin >> index;

cin >> data;

if(index[0] == 'q' || index[0] == 'Q' || data[0] == 'q' || data[0] == 'Q')

{

break;

}

i = atoi(index);

d = atof(data);

a[i] = d;

b[i] = 1;

cout << endl;

}while (true);

for(int k = 0; k < 100; k++)

{

if(b[k] == 1)

{

indexcount = indexcount + 1;

}

}

cout << "You stored this many values: " << indexcount << endl;

cout << "The index-value pairs are: " << endl;

for(int i = 0; i < 100; i++)

{

if(b[i] != double())

cout << i << " -> " << a[i] << endl;

}

do

{

cout << "Input an index for me to look up [Q to quit]: ";

cin >> index;

if(index[0] == 'q' || index[0] == 'Q')

{

break;

}

int i = atoi(index);

if(b[i] != 0)

{

cout << "Found it! ";

cout << "The value stored at " << i << " is " << a[i] << " ";

}

else

cout << "I could not find it. ";

}while(true);

return 0;

}

Input an index and a value [Q to quit]: 9 4 Input an index and a value [Q to quit]: 2000 -999 Input an index and a value [Q to quit]: You stored this many values: 3 The index-value pairs are: 5 - 10 9 -4 Input an index for me to look up [Q to quit]: 1000 Found it! The value stored at 1000 is -999 Input an index for me to look up [Q to quitj: 101 Found it! The value stored at 101 is -999 Input an index for me to look up [Q to quit]: 100 Found it! The value stored at 100 is -999 Tnput an index tor me to 100K up Le to quitj: I could not find it. Input an index for me to look up [Q to quit]: 2500 Found it! The value stored at 2500 is -999 Input an index for me to look up L0 to quitj: aq Process returned 0 (0x xcution time 71.011 s Press any key to continue. execution time: 71.011 s

Explanation / Answer

Program looks good except minor improvement

1) you need to initialize the array b with 0.0 because it took any garbage value and when try to comparing with b[i]!-=0 or b[i]==0 giving buggy result

2) for range thing you can compare the entered range with 0 to max length of array and if number not residing then warn user to input again.

I have modified only cpp code

//Header file

#ifndef StaticArray_h
#define StaticArray_h

template <typename V, int CAP>
class StaticArray
{
V values[CAP];
V dummy;


public:
StaticArray();
int capacity( ) const {return CAP;};
V operator[](int) const;
V& operator[](int);
};

template <typename V, int CAP>
StaticArray<V, CAP>::StaticArray()
{
for (int i=0; i < CAP; i++)
values[i] = V();

dummy = V();
};

template <typename V, int CAP>
V& StaticArray<V, CAP>::operator[ ](int index)
{
if (index < 0) return dummy;
if (index >= CAP) return dummy;
return values[index];
}

template <typename V, int CAP>
V StaticArray<V, CAP>::operator[ ](int index) const
{
if (index < 0) return 0;
if (index >= CAP) return 0;
return values[index];
}


#endif

//cpp code

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

#include <cassert>

#include "StaticArray.h"

#include "StaticArray.h" //ifndef test

int main()

{

//declaring the size in variable so if need to change only need to change one place

const int arr_len=100;

StaticArray<double, arr_len> a;

StaticArray<double, arr_len> b;

//need to initialize the array as when u declare it takes garbage values

for(int i=0;i<arr_len;i++){

b[i] = 0.0;

}

int indexcount = 0;

int i;

double d;

char index[10];

char data[10];

char search[10];

do

{

cout << "Input an index and a value [Q to quit]: " << endl;

cin >> index;

cin >> data;

if (index[0] == 'q' || index[0] == 'Q' || data[0] == 'q' || data[0] == 'Q')

{

break;

}

i = atoi(index);

//enforcing user to enter values in range

if(i<0 || i>arr_len-1){

cout<<"Invalid Index! Index must be from 0 to "<<arr_len-1<<endl;

continue;

}

d = atof(data);

indexcount++;

a[i] = d;

b[i] = 1;

cout << endl;

} while (true);

//no need of the below code as counting already doing above

/* for (int k = 0; k < 100; k++)

{

if (b[k] == 1)

{

indexcount = indexcount + 1;

}

}

*/

cout << "You stored this many values: " << indexcount << endl;

cout << "The index-value pairs are: " << endl;

for (int i = 0; i < 100; i++)

{

if (b[i] != double())

cout << i << " -> " << a[i] << endl;

}

do

{

cout << "Input an index for me to look up [Q to quit]: ";

cin >> index;

if (index[0] == 'q' || index[0] == 'Q')

{

break;

}

//enforcing user to enter value in range

int i = atoi(index);

if(i<0 || i>arr_len-1){

cout<<"Invalid Index! Index must be from 0 to "<<arr_len-1<<endl;

continue;

}

if (b[i] != 0)

{

cout << "Found it! ";

cout << "The value stored at " << i << " is " << a[i] << " ";

}

else

cout << "I could not find it. ";

} while (true);

return 0;

}

//Output

Input an index and a value [Q to quit]:
102
23
Invalid Index! Index must be from 0 to 99
Input an index and a value [Q to quit]:
98
12

Input an index and a value [Q to quit]:
45
11

Input an index and a value [Q to quit]:
23
12

Input an index and a value [Q to quit]:
q
q
You stored this many values: 3
The index-value pairs are:

23 -> 12
45 -> 11
98 -> 12
Input an index for me to look up [Q to quit]: 32
I could not find it.
Input an index for me to look up [Q to quit]: 19
I could not find it.
Input an index for me to look up [Q to quit]: 98
Found it!
The value stored at 98 is 12

Input an index for me to look up [Q to quit]: 45
Found it!
The value stored at 45 is 11

Input an index for me to look up [Q to quit]: q

//Please do let me know if u have any issue , or ur expectaion, will helping you out...