Category: computer science

  • How to write an algorithm to swap values ?

    /* part of work copied from http://www.sanfoundry.com/c-program-swap-values/ */ #include <stdio.h> swap_values() { int temp; int *ptr1, *ptr2; int a, b; printf(“Enter integer values to be swapped: “); scanf(“%d %d”,&a,&b); ptr1 = &a; ptr2 = &b; temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; printf(“Swapped values are: %d %d \n”,a,b); } /* http://www.programmingsimplified.com/c-program-swap-two-numbers */ /* […]

  • How to write an algorithm to find the number of bits set in a typical number ?

    #include <stdio.h> cbs () { unsigned int v; /* count the number of bits set in v */ unsigned int c; /* c accumulates the total bits set in v */ printf(“Enter v”); scanf(“%u”,&v); for (c = 0; v ; c++) { v = v & (v – 1); /* clear the least significant bit […]

  • How to write an algorithm to detect if two integers have opposite signs ?

    /* detect if two integers have opposite signs */ #include <stdio.h> int x , y; typedef int bool; bool f; int sdetect(x , y) { f = ((x ^ y) < 0); if ( f == 1 ) { printf(“They have opposite signs\n”); } else { printf(“They have same signs\n”); } }

  • What is the base-10 value of 11100101?

    What is the base-10 value of 11100101? [toggle title=”Answer” float=”left” width=”100px” margin=”0 20px 20px 0″ status=”closed”] 229 [/toggle] related source : SCHAUM’S OUTLINE OF Principles of COMPUTER SCIENCE related resource : http://www.wikihow.com/Convert-from-Binary-to-Decimal

  • Write the number 229 in base-2.

    Write the number 229 in base-2. 11100101 related source : SCHAUM’S OUTLINE OF Principles of COMPUTER SCIENCE related resource : http://www.wikihow.com/Convert-from-Decimal-to-Binary

  • Problem 2.3 from SCHAUM’S OUTLINE OF Principles of COMPUTER SCIENCE

    Count the primitive operations in your algorithm to find the mean. What is the order of growth of your mean algorithm? setup 4 loop 5 * length (length = n, the count of numbers to be averaged) return 2 Order of growth : Θ( n ) related source : SCHAUM’S OUTLINE OF Principles of COMPUTER […]

  • Write pseudo code for finding the mean of a set of numbers.

    mean( list_of_numbers ) length <– length of list_of_numbers index <– 1 sum <– 0 while index <= length { sum <– sum + list_of_numbers[index] index <– index + 1 } return sum / length example input : mean ( 1 2 3 4 ) length <– 4 index <– 1 sum <– 0 list_of_numbers[1] = […]