• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

A wonderful programming question from an AP Comp Sci student

TGCid

Platinum Member
First of the purpose of the program is to take what the user input into the array (1 2 3 4 5 6) and reverse the order then cout (6 5 4 3 2 1). I did pretty much the entire program except one part where I am stuck. The teacher said I cannot ask the user for the amount of inputs so the size of the array cannot be predetermine. Secondly I cannot limit or force the amount of inputs onto the user. Thirdly I cannot make the user indicates that he/she is finish by having them type in a character at the end, I would have to make the program so that it would know the user is finished when he/she press enter.

Here is a link to my program http://www.juston.com/public/seikend3/LAB2.CPP. Could you guys look at the code and tell me what I need to full filled the three requirements that are left? The program works fine except for the things that I indicated. Please keep the coding simple as we haven't got that far yet.

My email address is deus@san.rr.com btw if you wish to make the slight change and send me the program.
 
Mm.. I think you best be figuring it out yourself. Somebody doing your school assignment for you is not going to teach you anything.
 
I suppose if you were in Java 2 you could use a vector. Dont know if C has vectors.

Otherwise could you not just make the array a large number like 10,000 indexes? Doubtful anybody is going to input that many 🙂

-Merle
 
dl and Rudee,
I know you meant well but how am I suppose to learn something that was never taught to me or at least having examples to look at. It is like telling a person to learn how to multiply without explaining the concept and no sample.
 
Heh....yeh you should do this yourself.....

However, I will say that the best way to do with would be a "First In Last Out" stack (or heap if you prefer). Basically the last item pushed onto the stack would be the first item popped. However, this requires dynamic memory allocation. If you don't know how to do this then ignore what I just said 😉

Edit:
Hmmmmm.....just re-read your post. What I suggested above is much too complicated. All you need is a method of dynamically allocating an array of characters. If you don't know how to do this I think that VC++ has a dynamic String class in <cstring.h> that will handle the dynamic allocation for you.

Good Luck,
ERJ
 
are you supposed to be using an array or like a linked list? Seeing that it is only about 2 weeks into the school year i'll assume array. Just make a while loop. Have some sort of key like &quot;0&quot; to end input or something. So you have a counter that starts at 0 and you increment it in the while loop, and use the counter as the index of the array you are entering stuff in. So then at the end you will have some number in the counter say its 5. (that is what it would be in your example). So then you do a for loop like

for (int i=counter;i>=0;i--)
cout<< arrayname;


I would just make the array really big like 200 indices. Unless you can do it with linked lists, which is pretty similar anyways. I took CS AP for two years in high school, and we just sorta got left there with out books to do it. Some geometry teacher was teaching it and she knew nothing. SO i know how this guy feels.
 
Maybe you should tell us what language are you writing the program in? Like someone said before, if you use Java, you can use vector to dynamically allocate the array.

Edit: Never Mind, saw you are writing a C++ program
 
hmmm, dood, I was trying to help you out...did you visit the link?

here's another one: borland's teach yourself C/C++ in 21 days

Read that book and you'll be well on your way 🙂

btw, I can see not too many programmers replied...just look at the extension of his file...cpp is c++ 😀
 
Well thanks for the suggestions so far. I will try to sort all of this and figure things out....eventually. The teacher is not really explaining anything. The part that he does, I complete that part of the program easily. Problem is he is asking us to do things he never explained. The main program itself I know what to do, the problem lies with the teacher being picky on how the program should be like oh you can't do this cause I don't like it. I am able to code the program perfectly fine if he allows me to ask the user for the amount of inputs. But he doesn't and he wants us to do things that is we never learn.

dl,
I will read the sources you gave me, thanks.

 
Hey, you can check ENTER for the END OF INPUT.

Here is my suggestion :
1. Create an array of size 100 or something.
2. Read the input until you detected ENTER.
Keep a counter while you reading the input, if
this counter is GREATER than 100 (or the size
you set earlier) do the following :
- create new array of size twice the original array
- copy all the elements from the original array
to the new array
- set the new array as the original array.
All the above are in the reading input loops.

A rough reading the input algorithm looks like the following :

//--------------------------ALGORITHM START-------------------

create an array of size ARRAY_SIZE
counter = 0;
do
{
if (counter > ARRAY_SIZE)
{
create new array with size = 2*ARRAY_SIZE
ARRAY_SIZE = 2*ARRAY_SIZE
copy all the elements from original array to the new array
set new array to be original array
}


read the input into the array
increase counter

} while you haven't detected ENTER;

//----------------------ALGORITHM END------------------------

Easiest way would be using linked-list (and create a STACK, thus
it automatically reverse the list for you 🙂), but you haven't
learned this yet 🙁


edit : The above is called ARRAY GROWING technique.
 
Br0wn,
You da man. How do I detect when the user presses ENTER? This is the key to everything, I think I can do the rest.
 
use cin.get(), instead cin, to get immediate response on the input.
(you need to read in as character though)

Something like the following :
//---------------------CODE START-------------------

char a;
counter=0;

while(1)
{
cin.get(a); // same as cin >> a, except it read whitespace also

if (a == '\n')
{
// 🙂 you detected ENTER here, so quit the loop
break;
}
else if (a == ' ')
{
// ignore whitespace
}
else
{
// everything else is here
put a into the array.
counter++;
}

}

//--------------------CODE END----------------------


edit : should probably also tell you the following,
since you are reading a character, you need to convert
into integer.

A single digit conversion is easy,
supposed a contains 1 but of type character, then
we want to move to b which of type int.

b = a - '0'; // b will have 1 of type integer

for multiple digit conversion, expand the ideas 🙂
(if you can't do this, time to hit the books or the
links above that someone provided to you).
 
brown.. you hit it.. though I should have read this before.. I could have answered it..

Anyway I hate teachers who teach a bit and ask for a lot. I had this one CS teacher who basically states &quot;On a PC look for the F13 Key&quot;.

I looked at myself.. and said.. F13 on a PC.. Nah.. not possible.. He insisted that his PC had a F13 Key.. I said No Way.. there can be an F13 Key. Sure enough on a whim I followed him to his office.. and guess what it was a MAC.

So much for the F13 key.. it exists on MAC's not PC's.
That semester was H@LL.. Towards the end I stopped going to class... just showed up for the tests and assignment drop off's.

 
Back
Top