Search code examples
arrayslistsortingpseudocode

Is the logic of this pseudocode with arrays correct?


I am having some trouble converting this homework problem into pseudocode. I would like to know if the logic is correct.

The problem:

Write a program that prints the following elements: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, up till a user-entered value. The program should not print more than 10 numbers per line. The last line printed may be less than 10. Apart from the first number and the last number, there should be a “,” separating all the numbers. The last number on every line should have a “.” after it. Before exiting it should also prints the sum of all elements : Calculate the sum of the items and enter the result. For instance if the user entered 76 your output should be as follows A sample run of your program should be as follows:

Enter your number ( >= 1 and <= 100) : 76 Your sequence is

1, 3, 5, 7, 9, 11, 13, 15, 17, 19. 21,23, 25, 27, 29, 31, 33, 35, 37, 39. 41,43, 45, 47, 49, 51, 53, 55, 57, 59. 61,63, 65, 67, 69, 71, 73, 75.

The sum of the above elements 1444.

My attempt:

Algorithm ElementsSequenceSeries

// declare variables
Var num, count, odd, sum, arrayOne, arrayTwo, arrayThree, arrayFour, arrayFive

// initialize variables
Count = 0; sum = 0; odd=0; arrayOne = 0; arrayTwo = 0; arrayThree = 0; arrayFour = 0; arrayFive = 0;

<BeginAlg>

Print (Input 1<= num <= 100); 

Count = count + 1;
Odd = (count*2)–1; // create a list of odd numbers up to a user-defined value less than 100

If (num > odd)
  Print(Odd);

    // For the indices 0 to 8, place a comma after each element of the array.
    For (num <=20 and num> odd) and (arrayOne[9]= {0,1,2,3,4,5,6,7,8})
        Print (arrayOne[]= {0,1,2,3,4,5,6,7,8} + “,”);
    EndFor

    // For the index 9, place a fullstop after it.
    For (num <=20 && num> odd) and (array One[]= 9)
        Print arrayOne[]= 9 + “.”)
    EndFor


    For (num <=40 and num> odd) and (arrayTwo[]= {0,1,2,3,4,5,6,7,8})
        Print (arrayTwo[]= {0,1,2,3,4,5,6,7,8} + “,”);
    EndFor

    For (num <=40 and num> odd) and (arrayTwo[]= 9)
        Print arrayTwo[]= 9 + “.”)
    EndFor

    For (num <=60 and num> odd) and (arrayThree[]={0,1,2,3,4,5,6,7,8})
        Print (arrayThree[]= {0,1,2,3,4,5,6,7,8} + “,”);
    EndFor

    For (num <=60 and num> odd) and (arrayThree[]= 9)
        Print arrayThree[]= 9 + “.”)
    EndFor


    For (num <=80 and num> odd) and (arrayFour[]={0,1,2,3,4,5,6,7,8})
        Print (arrayFour []= {0,1,2,3,4,5,6,7,8} + “,”);
    EndFor

    For (num <=80 and num> odd) and (arrayFour []= 9)
        Print arrayFour []= 9 + “.”)
    EndFor


    For (num <=100 and num> odd) and (arrayFive[]={0,1,2,3,4,5,6,7,8})
        Print (arrayFive []= {0,1,2,3,4,5,6,7,8} + “,”);
    EndFor

    For (num <=100 and num> odd) and (arrayFive []= length)
        Print arrayFive []= length + “.”);
    EndFor

EndIf

Sum = arrayOne, arrayTwo, arrayThree, arrayFour, arrayFive;
Print(sum);

<EndAlg>

Solution

  • the algorithm proposed by you uses memory that can be saved, since we need to print all odd integers the problem can be solved using four variables, one for keeping track of sum, one for changing line,one for the user input and one as a looping variable.

    Variables:
    
    sum   :int   // keeps track of the sum
    count :int   // keeps track of the Integer printed in a line.
    i     :int   // looping variable.
    n     :int   // Given as input
    Algorithm:
    
    /* BEGIN ALGORITHM */
    n=input();
    count=0;
    sum=0
    for(i=1:i<n:i=i+2){
                      print("i,");
                      count=count+1;
                      if(count>9){ 
                                   print("/n"); // change line;
                                   count=0;
                                   }
                       sum=sum+i;
                       }
    print("n."); 
    print("Sum is (sum+n)");
    /* END ALGORITHM */