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~

You don't need a for loop to display the single value user wants. Simply test if the input value i is between 0 and size-1, inclusive. If so, display the array element at that index. You should wrap all this in a loop that continues as long as the input index is outside the correct range.

Also, as you have it written, two big mistakes occur. First, you use the input variable holding the user's choice as the loop counter, thus destroying the user's input. Second, your if statement is assigning size to i, which will cause the loop to end prematurely.

A third logic error is that the for loop, if written correctly, would not display any array elements at all, since you return if the loop variable is not equal to the array size, which it won't be unless the array is of size 0, oh, that would be an empty array!

How do I test something like ...

If(0<=i<size)
cout << i

Is this the test??

Also, as you have it written, two big mistakes occur. First, you use the input variable holding the user's choice as the loop counter, thus destroying the user's input. Second, your if statement is assigning size to i, which will cause the loop to end prematurely.

Okay if I change the loop variable to say 'j', how do I get it to continue.

You stated the loop was wrong, could you please tell me how the loop should look. I am not getting this at all.

I am very very new to C++. I want to learn, but it is simply not getting through. I need a step by step I think.

Where would you recommend I start?

How do I test something like ...

If(0<=i<size)
cout << i

Is this the test??

You cannot write this the way you might in math class, where you assert that a < b < c (it is true that a is less than b and that b is less than c), instead you must do the two tests that the description states) if( a < b && b < c ) { //it's true } For your problem, the only loop you need is on that keeps asking the user for the index of a value to display, as long as the user gives an invalid index. A do...while loop would be a good choice to use here.

Once he's entered a valid index, simply display that element of the array.

Work on that function, post your revision.

So I have something, but I am not getting the desired results.

Here is the function I tried to construct from my understanding of your instruction. I think I am getting close but something is wrong. Am I close?

void read_one_value(double A[ ], int size)
{
	int j=0; 
	int i=j;

	if( 0 <= j && j < size ) 
	{
		cout << "Enter an Array Index[0,1,2,...,12,13]:  ";
		cin >> j;
	}

	do {
				
				cout << A[i];
	}
	while(j=i);
			
		}//End of function

M

The way it is now, you effectively set j=0 , then test if( 0 <= j && j < size ) , which will always be true, since j will always be 0 at this point. You didn't need to add a new variable(j), you should just not alter the user input (i). I believe the previous poster meant a different place for the do..while . You have it around your output, it should be around your input. You might have to think a little about your test... hint: the test you have in your if is close. Also I think the previous poster mentioned that your output need not be in a loop. Remember, you are asking the user for the index to the array and once it gives you a valid number, you are ready to use it as-is. You're getting close, try again.

I am so fusterated right now. I am completely lost!!!

So First should be the input, then the test, then a loop???

What am I suppose to be do (doing) and what is while(while the input is equal to the index?)?


M~

I had a light bulb moment!!! I got the output working.

Now I need the loop to repeat if the input is not correct. Where should that be placed? Hint Please??

Here is what I have so far:

void read_one_value(double A[ ], int size)
{
	 
	int i;

	cout << "Enter an Array Index[0,1,2,...,12,13]:  ";
	cin >> i;

	if( 0 <= i && i < size ) 

	{
		cout << A[i];
	}
		

		
}
			
		//End of function

How does it look??

m~

Good work! So now you 1) ask for input 2) if the input is in the desired range, output (otherwise do nothing). What you want to do is ask for input while input is not in the desired range, then output..

You are the best!! Not only does it work you made me learn it!!!! I am so happy right now!! I have been working on this for hours (12+)!

Check it out!!!!!

void read_one_value(double A[ ], int size)
{
	 
	int i;

	do{
		cout << "Enter an Array Index[0,1,2,...,12,13]:  ";
		cin >> i;
	}
	while(i>size);
	

	if( 0 <= i && i < size ) 

	{
		cout << A[i];
	}

			
}
			
		//End of function

Oh no the input '7' does not produce the correct value

How do I (-1)? Am I thinking in the right direction?

M~

Another light bulb moment! I changed the while!

while(i>size-1);

I am cooking now!!! Thanks Bunches!

M~

Congratulations, but not quite there (really, really close though). You have the do--while in the right place, but the test is incomplete. What if someone enters -5? Also, what if the array size is 5 and someone enters 5? Finally, if you make sure that the user enters a valid number before leaving the while loop, there is no reason to test it again before you do the output. On your out put, you are saying "If i is between 0 and size-1, then cout". Your while should be saying "do ask for an index while the index is not between o and size-1"

sorry, missed your last couple, you figured out the size-1 part, now look at the rest.

I did and now the whole thing is off. Here is my mistakes

void read_one_value(double A[ ], int size)
{
	int i;

	do{
		cout << "Enter an Array Index[0,1,2,...,12,13]:  ";
		cin >> i;
	}
	while(i!=size-1);
	
	if( 0 <= i && i < size ) 

	{
		cout << A[i];
	}

			
}//End of function

Why can I not get this stuff??

M~

Is this the correct while -

while(i<0||i>size-1);

I think it might be????
M~

that looks right... and if that successfully weeds out the invalid index values , then you can eliminate the if statement altogether and just do your cout .

It successfully weeded out and I eliminated the if!!! It works and I am happy! Maybe I can sleep now! Thank you, thank you, thank you!

M~

Thank you for patience with this old bird!!! I am grateful!

Yes WOOHOO!!!!

Respectfully,

M

This question has already been answered. Start a new discussion instead.