c++, just couldn't type 1 small part.. please help in 3 hours..

...
Last edited on
Well.

Before the loop: add the variables int index and double value, set both to 0.
Within the loop: each iteration increase the index. Add the length to the value.
After the loop: if the index is greater 0 devide the value by that index

and you have the average.
...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
 int main(void){

 int s,n;
 int index = 0;
 double value = 0;
 do{
...

 index++;
 value += s;

...
 }while(true);

 if(index > 0)
  cout << index << " squares printed. Average length: " << value /  index  << endl;

 system("pause");
 return 0;
 }
Last edited on
Can you help to figure out this assingment??

I am trying to get positive integer numbers as inputs and checks to see if those numbers are palindromic. Like that;

Enter a positive integer (0 to exit): <1228>
1228 is not a palindrome

Enter a positive integer (0 to exit): <15851>
15851 is a palindrome

Enter a positive integer (0 to exit): <hello>
*** error: inputs must be an integer

Enter a positive integer (0 to exit): <919>
919 is a palindrome

Enter a positive integer (0 to exit): <-99>
*** error: inputs must be greater than zero

Enter a positive integer (0 to exit): <4115>
4115 is not a palindrome

Enter a positive integer (0 to exit): <0>
I tried something but couldn't finish all codes. I had trouble with filling the bool function.. How can I fill in that Function, and also does that code logical??

These are my codes..

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
#include <iostream>
using namespace std;

bool is_a_palindrome(char[]){
.
.
.
.
.
.
}


int main(int, char**){

const int x=80;
const int END_VALUE=0;
long int nbr;
char checkPal[x];

do{

    cout<<"Enter a positive integer ("<<END_VALUE<<") to exit: ";
    cin>>nbr;
    
    char checkPal=nbr;
    
        if (cin.fail()) {
                cout << "*** error: inputs must be an integer" << endl;
                continue;  }
              
        else if(nbr<0){
                cout<<"*** error: inputs must be greater than zero"<<endl;
                continue;  }
        else if(is_a_palindrome(char checkPal[x])){
                cout<<nbr<<" is a Palindrome "<<endl;        
        }
        else
                cout<<nbr<<" is not a Palindrome"
    
    
}
while(nbr!=END_VALUE);


system("pause");
return EXIT_SUCCESS;
}
The first thing is to convert to number to a string, then compare the first to last, second to second last, until you have no more elements or you have a middle element. Hope it makes sense :)

PS: there is a similar thread a a few posts away
Topic archived. No new replies allowed.