C++ Question

This is a question on my study guide and I believe the answer is: 543210
is this correct?

with the following input: 456789
what will print out?

int i,tp; int myary[6];
char khar;
for(i=0; i<6; i++)
{
cin.get(khar);
myary[1] = khar – ‘0’;
}
for(i=1; i<6; i++)
myary[i-1] = myary[i-1] + myary[i];

for(i=0; i<6; i++)
cout<<myary[i];
cout endl;
It would be interesting to hear how you got this result.
Last edited on
my array holds 6 values and I know it prints in reverse but I wasn't sure if it was just 54 or I had to let it hold 6 places in my array. I am unsure that is why I am asking for help
I do not see where the array is printed in the reverse order.
closed account (Dy7SLyTq)
there are too many errors in that first of all
well idk it is a question on my study guide
It is strange that your guide contains so many typo.:)
For example in this code snip

for(i=0; i<6; i++)
{
cin.get(khar);
myary[1] = khar – ‘0’;
}

only the element with index 1 (one) is assigned.

And here

cout endl;

you omitted operator <<.

If do not take into account the typo this loop

for(i=1; i<6; i++)
myary[i-1] = myary[i-1] + myary[i];

does the following

myary[0] = myary[0] + myary[1];
myary[1] = myary[1] + myary[2];
myary[2] = myary[2] + myary[3];
myary[3] = myary[3] + myary[4];
myary[4] = myary[4] + myary[5];

So you can calculate yourself what values the array will have.
Last edited on
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

using std::cout;
using std::cin;
using std::vector;

int main(int argc, char *argv[])
{
	vector<int> Container(1);
	int Counter = 0;
	
	while(cin>> Container[Counter++])
		Container.resize(1);
		
	vector<int> Copy(Container.size() - 1)
	
	for(auto I : Container)
		Copy[Copy.size() - I] = Container[i];
		
	for(auto I : Copy)
		cout<< Copy [I] <<" ";
}


edit: accidently put a 1 where an I should be in the first for loop
Last edited on
@DTSCode


As I already said you understand nothing in programming and as the result your advices and comments do more harm than good.

This stupid code

1
2
	while(cin>> Container[Counter++])
		Container.resize(1);


will result in a program abort.
closed account (Dy7SLyTq)
oh really and how is that?

[edit] im being sarcastic my code is fine. im merely humoring you while you try to prove me wrong. i know my code works thought [/edit]
Last edited on
@DTSCode

oh really and how is that?


As I said in other thread I am not your professor. So try to understand it yourself or wait until someone experienced will point out the bug.
Last edited on
closed account (Dy7SLyTq)
thank god you arent my professor you cant code to save your life. as i said i was being sarcastic because there is no bug
I will not even speak about that code shall not be compiled. There is no any sense to spend time to such genius as you.:)
closed account (Dy7SLyTq)
really because i just compiled it. the only issue (and i typed it on the fly so thats why i made 1 mistake) was i made a 1 where an I should be. it compiled and ran fine
It seems that you are incurable.
Without any compiler I see that variable i was defined nowhere.

Copy[Copy.size() - I] = Container[i];

Change your brain!
Last edited on
closed account (Dy7SLyTq)
you know what vlad? when you learn to code come talk to me. i will just ignore you until then because so far you have been arguing with defenses poorer than a third world country and at times given no defense at, just hoping that i will take your current assertion as fact. i know im right and i dont need to the approval of a lowly forum troll who must take his frustration out on those stupid enough to lower themselves to your level. (i am unfortunately referencing myself). so good day. i really hope you can one day support your arguements
Topic archived. No new replies allowed.