how does this work?

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
45
46
47
48
49
50
51
52
53
54
55

#include<iostream>
using namespace std;

int main() {
    
int number;
int highest = 0;
int max_num;
int num[max_num];


cout << "this program determines which number is the highest." << endl;
cout << "please enter the max number." << endl;
cin >> max_num; // this is the list of numbers you can write.

for (int i = 1;i < max_num;i++) {
    cout << "enter numbers:" << endl;
    cin >> num[i]; // write the list of numbers.
    
}


for (int i = 1;i < max_num;i++) {
    
        
            
        if (num[i] > highest )  {
        
        highest = num[i];
        }
        

        
        
        
        
        
        }
    



cout << highest << endl;




system("pause");
return 0;
}






hello there fellow programmers.I just need to ask a question.My program works the way it needs to,but I just have one question....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int highest = 0; // .....


for (int i = 0;i < max_num;i++) {

if (num[i] > highest ) {

highest = num[i]

}

}






how does this work? I discovered this by accident and I don't have any idea how these lines of the code works.So I reckoned 'highest' containes every value of the array.So I did and experimented.


1
2
3
4
5
6
7
8

for (int i = 0;i < 10;i++) {

cout << largest;


}



and the output was,,,



0
0
0
0
0
0
0
0
0






so no,it just stays as '0' through the whole loop.So how on earth does it work?I hope you understand my question.
okay,I think I get the hang of it,now see if my theory is right,,,,


when the 'highest' is compared and the num[i] becomes the 'highest.'The loop is continued until you can no longer register the 'highest.'

am I right?
Last edited on
This is where the magic happens, your "test" output using the variable largest makes no sense with the code you've posted thus far.

1
2
3
4
5
6
7
8
9
int highest = 0; //So highest == 0 right now
for (int i = 1;i < max_num;i++) 
{         
   if (num[i] > highest ) //What happens if num[i] == 2 here, we enter the if block and assign highest to 2.
   //Repeat this loop until max_num, if num[i] is greater than highest again assign highest that new value
   {
      highest = num[i];
  }
}
Do you just not care about whatever is in num[0]?
@ResidentBiscuit

Yeah.I was in hurry when I made this :D

thank you,this will be resolved after 3 days. :D
I'm beginner in C++

Will you please help about my assignment? I don't know why it will not display.
Plz help.

# include <windows.h>
# include <iostream>
# include <string>
# include <conio.h>
# include <stdio.h>

using namespace std;

void gotoXY(int x, int y);
char num1[1000];
char num2[1000];

bool n = true;

int a, opt1, opt2;
char c, opt,t;

int main () {
do {
// print the box and the numbers
gotoXY(0,0); cout << char(201);
gotoXY(40,0); cout << char(187);
for(int i=1; i<40; i++)
{
gotoXY(i,0); cout << char(205);
gotoXY(i,16); cout << char(205);
}
gotoXY(0,16); cout << char(200);
gotoXY(40,16); cout << char(188);
for(int j=1; j<16; j++)
{
gotoXY(0,j); cout << char(186);
gotoXY(40,j); cout << char(186);
}
for(int k=1; k<40; k++)
{
gotoXY(k,3); cout << char(205);
}



gotoXY(1,4);
cout << "7 \t 8 \t 9 \t * \n";
gotoXY(1,6);
cout << "4 \t 5 \t 6 \t / \n";
gotoXY(1,8);
cout << "1 \t 2 \t 3 \t . \n";
gotoXY(1,10);
cout << "0 \t + \t - \t = \n";

gotoXY(1, 12);
return 0;


gotoXY(2,2); cin >> num1;
gotoXY(2,3); cin >> opt;
gotoXY(2,4); cin >> num2;
if((c-'0') >= 0 && (c-'0') <= 1000)
{
if(a <1000)
{
if(n==true)
{
num1[a]=c;
gotoXY(2,2); cout << num1;
}
else
{
if((c-'0') >= 0 && (c-'0') <= 1000)
{
if(a <1000)
{
if(n==true)
{
num2[a]=c;
gotoXY(2,4); cout << num2;
}
}
}
}
a++;
}
}
else if(c=='+' || c=='-' || c=='*' || c=='/')
switch(opt)
{
case '+': opt1 + opt2;
gotoXY(2,3); cout << "+";
break;
case '-': opt1 - opt2;
gotoXY(2,3); cout << "-";
break;
case '*': opt1 * opt2;
gotoXY(2,3); cout << "*";
break;
case '/': opt1 / opt2;
gotoXY(2,3);cout << "/";
break;
}
else if(c=='=')
{
if(opt=='+')
{
gotoXY(2,6); cout << opt1 + opt2;
}
else if(opt=='-')
{
gotoXY(2,6); cout << opt1 - opt2;
}
else if(opt=='*')
{
gotoXY(2,6); cout << opt1 * opt2;
}
else if(opt=='/')
{
if(num2==0)
{
gotoXY(2,6); cout << "ERROR!";
}
else
{
gotoXY(2,6); cout << opt1 / opt2;
}
}
}
else if(c==27)
{
gotoXY(2,2); cout << "______________";
}
else
{
gotoXY(2,2); cout << "ERROR!";
}
}while(c!=t);
gotoXY(2,2); cout << "_______________";
}


void gotoXY(int x, int y)
{
//Initialize the coordinates
COORD coord = {x, y};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}


Last edited on
Topic archived. No new replies allowed.