Urgent !!!Plzzz Help me my assignment!!! Pseudocode for an algorithm - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Urgent !!!Plzzz Help me my assignment!!! Pseudocode for an algorithm

charcoalcharcoal Posts: 2Member
pls help me Gentle men and ladies


Question 1

Write the pseudocode for an algorithm that receives an integer and then prints the number of digits in the integer and the sum of the digits. For example, given 13254 it would print that there are 5 digits with a sum of 15.

Question 2

Design an algorithm that tests whether or not two input lists of size n have at least one element in common. Give formulas for best case b(n) and worst case w(n) for your algorithm.



Thanks so much help me pls :(

Comments

  • hedonisthedonist Posts: 2Member
    [code]
    int n = 123;
    int remainder = 0;
    int arr[20];
    int counter = 0;
    n = fabs(n);
    while(n>0) {
    remainder = n%10;
    arr[counter++] = remainder;
    n = n/10;
    }
    [/code]

    sum = sum of all the elements in the array arr
    Number of elements = counter +1


    This is a very inefficient algorithm. Since you didnt mention any complexity, i didnt bother to think about the solution.If incase you have any efficiency criteria, do let me know.
  • hedonisthedonist Posts: 2Member
    let the two arrays be a[n] and b[n].

    [code]
    int i =0;
    int j=0;

    for(i=0;i<n;i++) {
    for(j=0;j<n;j++) {
    if(a[i] == b[j]) break;

    }
    }

    [/code]

    Best Case = When the first element is same = O(1)
    Worst Case = O(n^2)
Sign In or Register to comment.