...
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
 ``12345678910111213141516171819202122`` ``````#include 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..

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include 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 ("<>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"<
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.