Question

In: Computer Science

Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...

Translate the following C++ code to Pseudocode:

int main()
{
stack<char> stk,stk2;
int length =0,ele;
while(cin>>ele)
{
stk.push(ele);
length++;
}
if(length%2)
stk.pop();
for (int i=0;i<length/2;i++)
{
ele=stk.top();
stk.pop();
stk2.push(ele);
}
int flag=1;
for(int i=0;i<length/2;i++)
{
if(stk.top()==stk2.top())
{
stk.pop();stk2.pop();
}
else
{
flag=1; break;
}
}
if(flag==1) cout<<"NOT palindrome";
else
cout<<"palindrome";
}

Solutions

Expert Solution

Palindrome program in C++

A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.

Palindrome number algorithm

  • Get the number from user
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print palindrome number
  • Else print not palindrome number

Pseudo code is a term which is often used in programming and algorithm based fields. It is a methodology that allows the programmer to represent the implementation of an algorithm. Simply, we can say that it’s the cooked up representation of an algorithm. Often at times, algorithms are represented with the help of pseudo codes as they can be interpreted by programmers no matter what their programming background or knowledge is. Pseudo code, as the name suggests, is a false code or a representation of code which can be understood by even a layman with some school level programming knowledge.

Algorithm: It’s an organized logical sequence of the actions or the approach towards a particular problem. A programmer implements an algorithm to solve a problem. Algorithms are expressed using natural verbal but somewhat technical annotations.

Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and informative text written in plain English. It has no syntax like any of the programming language and thus can’t be compiled or interpreted by the computer.

pseudo code for palindrome algorithm :

  1. //beginning =start of sequence  and end=end of sequence
  2. start the program
  3. read the input from user where length is initally 0
  4. int length =0,ele;
  5. // create a stack that reads characters
  6. stack<char> stk,stk2;
  7. // compare the length  with stack characters
  8. while(cin>>ele)
  9. //loop(data not EOF ans stack not full)
  10. push(data)
  11. stk.push(ele);
  12. //increment the count of length of palindrome
  13. length++;
  14. //whlie loop ends
  15. //if condition length divides with 2
  16. if(length%2)
  17. //loop while stack is not empty
  18. pop(data)
  19. stk.pop();
  20. //add each character to string
  21. //for(current=beginning;current/2<end;current++)
  22. for (int i=0;i<length/2;i++)
  23. //data is inserted into top of stack
  24. ele=stk.top();
  25. //last element is deleted using pop operation
  26. stk.pop();
  27. //another stack data is inserted using push opeartion
  28. stk2.push(ele);
  29. //in if condition stack 1element is compared with stack 2
  30. if(stk.top()==stk2.top())
  31. {
  32. //from both stacks pop is performed
  33. stk.pop();stk2.pop();
  34. }
  35. //end of loop
  36. if(flag=1)
  37. //if palindrome ends in mismatch remove and decrement the size of palindrome
  38. //characters are not equal
  39. cout<<"NOT palindrome";
  40. //if both stack (data) are same print
  41. else
  42. //characters are equal
  43. cout<<"palindrome";
  44. }

Related Solutions

How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT