Question

In: Computer Science

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]);

Solutions

Expert Solution

Here is the code with the proper comments:

Code:

//importing the necessary packages needed for the code
#include<bits/stdc++.h>
using namespace std;
void swap(int& x, int& y)
// Function to swap the values, which is passed by reference
{
int z = x;
x = y;
y = z;
}
int main()
{
int iMin = 0,i = 0, j=0, n=0;
//variable declaration
cin>> n; // taking the size of the array from the user
int a[n]; //declaration of the array
for (int k=0;k<n;k++) // taking the array values from the user and display it on screen
{
int x=0;
cin>>x;
a[k] = x;
cout<<a[k]<<" ";
}
cout<<"\n";
// leaving the space for showing the better output
  
for(j = 0; j < n - 1; j++)
// Starting of your Pseudocode arrange the for loops through the brackets
{
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]);
// calling of the function
  
}

  
}
for (int k=0;k<n;k++)
// printing the final array to see the manipulation made by the program.
{
  
cout<<a[k]<<" ";
}


}

Output:

Input from the user, the user needed to enter the array size and array elements values.

5   
9 4 5 7 2

code output for the above inputs:

9 7 5 4 2

Please see the indentation while pasting this code at your own end:

Screenshot of the running code Input and output just for the reference.

Inputs:

The functionality of the code might be converting the middle elements of the array in the descending order. But this code produces the same output as a logic shared in the form of pseudocode.


Related Solutions

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...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
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.
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                      ...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
I have to translate C++ code into MIPS. It is expected to have an output of:...
I have to translate C++ code into MIPS. It is expected to have an output of: Value of a: 25 Value of b: 31 Value of c: 18 Value of d: 49 Here is the C++ code: I need to translate this C++ into MIPS code. #include using namespace std; int main(void) { int a = 5; int b = 6; int c = 7; int d; d = -1; if ( a < 10){ a++; }else{ a--; } d...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT