Question

In: Computer Science

Implement (provide pseudocode) the 'A la Russe' algorithm which does not use arrays.

Implement (provide pseudocode) the 'A la Russe' algorithm which does not use arrays.

Solutions

Expert Solution

PSEUDOCODE

russe(x, y)
begin
while (x >= 1) do
    if (x mod 2) then
       mult = mult + y;
    endif
x = x/2;
y = y*2;
end
return mult;
end

Example

CODE IN C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  /* getopt */

void help () {
 printf ("Use mode: 
");
 printf ("      $ ./russe -[-hva] x <number1> -y <number2>  
");
 printf ("      -h: show this help
");
}

int mrusse (int auxa, int auxb) {
  int mult = 0;
  while (auxa >= 1) {
    if ((auxa % 2) == 1)  {
      mult += auxb;
    }
    auxa = auxa >> 1;
    auxb = auxb << 1;
 }
 return mult;
}

int main (int argc, char **argv) {
 int x, y;
 int flgn1 = 0, flgn2 = 0;
 int opt;
    while ((opt = getopt(argc, argv, ":havx:y:")) != -1) {
      switch (opt) {
        case 'h': 
           help();
          return 0;
          break;
        case 'x': 
         flgn1 = 1;
         x = atoi(optarg);
         break;
       case 'y': 
         flgn2 = 1;
         y = atoi(optarg);
       break;
         default: /* '?' */
         help();
         exit(EXIT_FAILURE);
     }
  }
  if ((flgn1 == 0) && (flgn2 == 0)) {
     fprintf (stderr, "Error in numbers
");
     return -1;
  } 
  int mult = mrusse(x, y);
  printf ("     %i * %i = %i
", x, y, mult);
  return 0;
}

OUTPUT

$ ./russe -x 35 -y 91


Related Solutions

Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode...
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode for a solution of the following problem. This solution will include the use of arrays needed to complete all parts of the logic. You have requested to develop a program that will record and process the rainfall totals of a 12 month period. You would use an array to store each months total. Once all 12 months amounts are entered then your solution need...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm and quick union algorithm
Problem 1: Describe using pseudocode and implement an efficient algorithm for finding the ten largest elements...
Problem 1: Describe using pseudocode and implement an efficient algorithm for finding the ten largest elements in an array of size n. What is the running time of your algorithm? Problem 2: An array A contains n-1 unique integersin the range [0, n-1]. There is one number from this range that is not in A. Design a O(n) algorithm for finding that number. Describe the algorithm in pseudocode. Implement the algorithm in Java. Hint : Consider computing a function of...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your program takes 2 seconds to run, collect running times for your algorithm using the following string lengths: p/4, 2p/4, 3p/4, p, 5p/4, 6p/4, 7p/4, 2p. c)Generate your strings by reading the attached file, only reading as many characters as you need. d)Plot your results (x-axis is string length, y-axis should be time) e)Draw conclusions based on your graph. You may also need to plot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT