Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

array input loop

Posted on 2003-12-05
6
Medium Priority
?
306 Views
Last Modified: 2010-04-02
enter 3 digits into each of the array value's and keep entering 3 digits until we enter a digit of 600 which then stops us entering to the array and will then print the array on screen, it looks like it could be a really simple solution but i cant see it.

here is what we have so far:

void main()
{

            const int max = 100;

            int a[ max ];

            int instruction;

                  cin >> instruction;

                  a[ max ] = instruction;

                  while ( instruction!=600 )
                  {

                        for ( int i =0; i < max; i++ )
                              
                              a[ max ] = instruction;

                                    i++;

                        cin >> instruction;

                  }

                        for ( int j = 0; j < max; j++ )  
                        
                              cout << setw( 7 ) << j << setw( 13 ) << a[ j ] << endl;

                                    
}
0
Comment
Question by:a_migdal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 90 total points
ID: 9883652
(1) Look at the dimensionality of your loops:
    while ...
      for ...

    for ...

The first pair of loops are nested and process a two dimensional structure. a is a one dimensional structure. There is a problem.

So, which loop should go?

Since you read until you see 600, you need a while loop.

You also need a counter to count the number of instructions that have been stored in the array.

You also need to store the 600 in the array so you really need a do ... while () loop. Something like:

  do
    // read in instruction
    // increment number of instructions counter
    // store the instruction at a[number of instructions counter]
  while (instruction != 600);

(Since this is homework I won't write the code for you).

Make sure you declare your counter before the do while.

(2) This does have one potential flaw in that if the number of instructions exceeds max you will have a buffer over run problem. I will ignore that for the moment unless you feel it is important.

-bcl
0
 
LVL 2

Assisted Solution

by:kmalhotra
kmalhotra earned 60 total points
ID: 9884502
void main()
{

      const int max = 100;
      int a[ max ];
      int instruction, count;
      count = 0;
      
      do {
            cin>>instruction;
            a[count] = instruction;
            count++;
      }while (instruction != 600);

      cout<<"-----------------------"<<endl;
      for (int ctr = 0; ctr < count; ctr++) {
              cout << setw( 7 ) << ctr << setw( 13 ) << a[ ctr ] << endl;
      }
}
0
 
LVL 7

Expert Comment

by:jj819430
ID: 9884959
don't use an array for this. You will be running into memory problems like bcladd said.
I am going to edit kmalhotra's submission so  you won't have any issues with the memory.

void main()
{

     string a = "";
     int instruction, count;
     count = 0;
     
     do {
          cin>>instruction;
          a += instruction;
          count++;
     }while (instruction != 600);

     cout<<"-----------------------"<<endl;
     for (int ctr = 0; ctr < count; ctr++) {
            cout << setw( 7 ) << ctr << setw( 13 ) << a[3*ctr] + a[3*ctr +1] + a[3*ctr + 2] << endl;
     }
}


All this is is putting it into a string. It will automatically deal with any memory allocation needs because it is really a char* and it using pointers. Much simpler for your project then actually writing a linked list or something like that.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Expert Comment

by:jj819430
ID: 9884967
Oh, kmalhotra
I hope you don't mind my working with the code you already posted.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9885209
jj819430-

I think the original poster wants to be able to extract the instructions as units. Your suggestion to use an STL container is a good one: I think a vector of int would be more appropriate in this case (look at poster's other open questions for some background).

-bcl
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9894436
Just change the part of your code where you index.

You put max to index with. An array's first index is 0

               a[ max ] = instruction;

Should be something like

              a[current_index] = instruction;

Then you did it again.

               while ( instruction!=600 )
               {

                    for ( int i =0; i < max; i++ )
                         
////////////////max....
                         a[ max ] = instruction;

                             
Also, you should not copy other persons code here as you should know the code before you advance. Teachers will know you have no clue and give you the F.

Another thing you want to prompt inside your loop to get the values

               while ( instruction!=600 && instruction < max )
               {

                         
                   clrscr();//conio.h
                   cout << "Enter value ";
                   cin   >> instruction;
     
                   a[ i ] = instruction;

              }//end loading

the var i now has how many instructions got loaded. Hmmm, you could use that value in another loop to print them.
while(x < i)
{
print...
}

RJ
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question