The complete output of the statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

double x = 974.23;
namespace one 
{
double x = 56.1102;
}	
namespace two 
{
double x = 73.8924;
}

using std::cout;		using std::cin;
void atEnd1(void) {cout << "Bye, bye!\n";}
void atEnd2(void) {cout << "Bye, bye, bye!\n";}

int main(int argc, char *argv[]) 
{
	atexit(atEnd1);	atexit(atEnd2);
	double x = 11.001;	short unsigned int i = 0;

	cout << "This program ran with " << argc << " input arguments.\n ";
	cout << "Do you know " << argv[2] << "?\n";
	printf("Here is the second argument:  %7.2f.\n",atof(argv[1]));
	cout << "What will argv[1]+2 be? --> " << argv[1]+2 << "\n";

	cout << one::x << " " << two::x << " " << ::x << " " << x << "\n";

	std::vector<double> myVals(8,1.618);
	for(i=0; i<myVals.size(); i++)
	myVals[i] += i+1;
	std::vector<double>::iterator vIt = myVals.begin();
	double weirdMax = *(std::max_element(myVals.begin(),myVals.end()));
	weirdMax += x;
	cout << "weirdMax = " << weirdMax << ".\n";
	cout << "The size and capacity of myVals are, respectively: ";
	cout << myVals.size() << " and " << myVals.capacity() << ".\n";

return 1;

}


I want to know the output of this code. I tried to compile it but it just stopped after line 26.

Anyways other than that.
here's my questions
1) does line 23 output anything? does it output line 17 and line 18's couts?
2)in line 25 cout<<argc is it 2?
3) what is the output of argv[2]? My guess it means the second argument which is atEnd2 which is Bye, bye, bye!
4)i have no idea what line 27 do. I know it set the field width to be 10 and precision to be 2. But i don't nkow what does atof(argv[1])'s output
5) line 30 ::x and x output are both 11.001? and does line 30 output be affected by line 27?
6)what does line 32 do? to be exact what dose myVals(8,1.618) do? does it create a vector array of 8 and fill it with 1.618?
7)38~40's cout statements i can't plow through it.
8)line 28 the cout should be e bye!? becuase it's + so i think it's 2 sppace ahead.
Last edited on
You'll learn better if you see for yourself. Post the errors you got when compiling and we can help out getting it to compile.

FYI, the atexit() function needs you to #include <cstdlib>.
The problem is i can't view the errors. IT saids the program stops working correctly, it seems this program traps itself in an infinite loop. Or that's what i thought so i comment out those for loops and the problem still present.
Last edited on
argv[2] is the second command line argument. You have to run your program with at least 2 arguments.
Last edited on
So argv[2] is actualyl the third argument that actually doesn't exist? becuase comptuer code count 0, 1, 2 since we have two argument i should change all the 1s to 0 and all the 2s to 1?(i was wrong lol i tried it the problem still present)
Last edited on
argv[0] is the name of the program
Ahh the most i ask i get more confused. So in short what is the output of line 25 and 26?
This program ran with [the number of arguments it's run with] input arguments.
Do you know [whatever the second argument is]?
Last edited on
Sigh. argv[0] is the name of the program any arguments following that will be populated into argv[1], argv[2] ... etc.

So on a linux terminal you execture your program with:
./program random argument

argv[1] is "random"
argv[2] is "argument"
Last edited on
Topic archived. No new replies allowed.