I am trying to write a function that will read one value of an array. I must prompt the user for an index and then read the its value. I am having a horrible time and need help.
// read_one_value(A, n) prompts the user for an index i and
// then reads a value into A[i]. Before reading A[i], we
// force the user to reenter i if it is out of bounds.
// In other words, i must be in the range: 0<=i<size.
void read_one_value(double A[ ], int size)
{
int i;
cout << "Enter an Array Index[0,1,2,3,4,5,6]: ";
cin >> i;
for(i=0;i <= size;i++)
if(i = size)
cout << A[i];
else
return;
}
//End of function
Here is the program:
#include <iostream>
using namespace std;
// Function prototypes
void read_one_value(double A[ ], int size);
void print_double_array(double A[ ], int size);
#define TASK 4
int _tmain(int argc, _TCHAR* argv[])
{
// Define and initialize array A.
const int SIZEA = 5;
int A[SIZEA] = {2, 4, 6, 8, 10}; // Initializer list: See Task 1.
#if TASK != 4
int i;
#endif
#if TASK == 1 || TASK == 2
// Print out the values of A[0...4]. (I.e., A[0], A[1], A[2],
// A[3], and A[4]).
//
for (i = 0; i < SIZEA; i++) {
cout << "A[" << i << "] = " << A[i] << " ";
}
cout << endl;
#endif
#if TASK == 2
A[-1] = 123;
// / Print out the values of A[-1] & A[5]
cout << "A[-1] = " << A[-1] << endl;
cout << "A[5] = " << A[5] << endl;
// End of Task 2 code
#endif
#if TASK == 3
//Task 3a
int B[SIZEA] = {1, 2, 3, 4, 5};
for(int i=0; i < SIZEA; i++)
A[i] = B[i];
//Output array A using a loop
for (i = 0; i <= SIZEA; i++)
cout << "A[" << i << "] = " << A[i] << endl;
//Task 3b
cout << A << endl;
//End of Task 3
#endif
#if TASK >= 4
// Task 4a
const int SIZED = 7;
double D[SIZED] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
#endif
#if TASK == 4
//print_double_array(D, SIZED);
//Task 4b
read_one_value(D, SIZED);
print_double_array(D, SIZED);
#endif
#if TASK == 5
for (i = 0; i < SIZEA; i++) {
cout << "Location of A[" << i << "] is " << &A[i] << endl;
}
cout << "\nIntegers are " << sizeof(int)
<< " bytes long on this machine.\n" << endl;
for (i = 0; i < SIZED; i++) {
cout << "Location of D[" << i << "] is " << &D[i] << endl;
}
cout << "\nDoubles are " << sizeof(double)
<< " bytes long on this machine.\n" << endl;
//Task 5b - the same for short's and float's
#endif
return 0;
}
// print_double_array(A, n) prints out A[0...n-1].
//
void print_double_array(double A[ ], int n) {
int i = 0;
//cout << "Printing array: " << A[0];
for (i = 1; i < n; i++) {
//cout << ", " << A[i];
}
cout << endl;
}
#if TASK == 4
// read_one_value(A, n) prompts the user for an index i and
// then reads a value into A[i]. Before reading A[i], we
// force the user to reenter i if it is out of bounds.
// In other words, i must be in the range: 0<=i<size.
void read_one_value(double A[ ], int size)
{
int i;
cout << "Enter an Array Index[0,1,2,3,4,5,6]: ";
cin >> i;
for(i=0;i <= size;i++)
if(i = size)
cout << A[i];
else
return;
}
//End of function
#endif
Please help as I have been working on this for hours.
M~