Help with program segment

Hello, I would like for some further instructions on how to write this program segment. It asks to print out the index of the smallest and largest character in an array called letters of 20 characters.

I do NOT want the assignment done for me, I only ask for further insight of how to go about solving it.
Thanks in advance.
1
2
3
4
5
6
int max = 0;
for(int i = 0; i < arraysize; ++i)
    if (array[i] > array[max])
        max = i;

std::cout << "largest element index: " << max;
Thanks for the reply (MiiNiPaa). I tried to do the complete question with the info you provided and came up with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
int max = 0;
 int min=0;
 int letter[20];

 for(int i = 0; i < 20; ++i)
 {
  if (letter[i] > letter[max])
      max = i;
  if (letter[i] < letter[min])
     min=i;
}  
 cout << "largest element index: " << max<<endl;
 cout << " samllest element index" <<min<<endl;       
 system("PAUSE"); 
 return EXIT_SUCCESS;   
}


I think something is wrong but i dont know what it is.
Last edited on
There is nothing wrong. Well, except for the fact that the problem said letters is supposed to be a character array, and you did not assign anything to said array.
Do you mean like to change int letter [20]; to char letter[20] or should i do something like declare 20 letters for the letter array like letter={a,b,c,d....};
Last edited on
Probably both, it depends on what do you want from your program.
Now you are sorting an uninitialized array, or array which contains some undefined data.
So would it be something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
 char data[]={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t};//this keeps giving error, did i even do i right?
 char letter[20];
 int max = 0;
 int min=0;
 

 for(int i = 0; i < data; ++i)
 cout<<data[i]<<endl;
 {
  if (letter[i] > letter[max])
      max = i;
  if (letter[i] < letter[min])
     min=i;
}  
 cout << "largest element index: " << max<<endl;
 cout << " samllest element index: " <<min <<endl;     
 system("PAUSE"); 
 return EXIT_SUCCESS;   
}
char data[]={'a', 'b', //...
Characters should be encased in single quotes.
Ok but now I have a problem with the if statements. I know im not doing it correct so what am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
 char data[]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'};
 char letter=20;
 int max = 0;
 int min=0;
 

 for(int i = 0; i < letter; ++i)
 cout<<data[i]<<endl;//shows the array data
 
//Im not sure what to do here.
 {
  if (letter[i] > data[max])
      max = i;
  if (letter[i] < letter[min])
     min=i;
 }  
 cout << "largest element index: " << max<<endl;
 cout << " samllest element index: " <<min <<endl;
     
 system("PAUSE"); 
 return EXIT_SUCCESS;   
}
You inserted another line between for() and opening bracked.
Delete line 10 or move it inside brackets.
Lines 14 and 16, you're trying to index letter, but letter is not an array.

Your cout at line 10 is now the only thing executed by your loop.

Lines 14. You don't want to use max as your subscript.

Line 16. You don't want to use min as your subscript.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    char letters[] = "abcdefghijklmnopqrst";
    int max = 0;
    int min = 0;

    for (int i = 0; i < strlen(letters); ++i)
    {
        cout << letters[i];
        if (letters[i] > letters[max])
            max = i;
        if (letters[i] < letters[min])
            min=i;
    }
    cout << "largest element index: " << max<<endl;
    cout << "smallest element index: " <<min <<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
ok and if (letter[i] > data[max]) is saying invalid types 'char[int]' do you know what that means?
In your second message, array was named letter. in second to last you renamed it to data but doesn't change other entries. change all instances of letters to data
It worked! and it returns 19 as largest element index and 0 as smallest. Is that correct?
Topic archived. No new replies allowed.