Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Can anyone help me with some basic programming homework?

  1. Nov 9, 2008 #1
    This is my homework for an introductory computer science class, and I am really not very good at this.

    If you can be of any help on ANY of these questions, I would greatly appreciate it. I don't even know where to start on almost any of them! Thank you!!!!


    1. Written Section (Short answers). Write these electronically in a text file
    that you will include in the homework submission.

    a) Describe in your own words why the standard C libraries help improve
    portability of the C programming language.

    b) In big-O notation, what is the running time of the arrayMax() function
    from the homework 2 solutions?

    c) Describe in your own words what the * and & operators in C are and how they
    are different.

    2. Bit counter. Submit your .c code file

    (Exercise 11-4 from Practical C Programming, Steve Oualline)
    "Write a program that counts the number of bits set in an integer. For example,
    the number 5 (decimal), which is 0000000000000101 (binary), has two bits set."

    More specifically, write a function, countBits() that takes an int as an
    argument and returns an integer count of the number of set bits. Then write a
    main function that reads an integer from the command line and outputs the
    return value of countBits() to the screen.

    3. Array addresses. Submit your .c code file

    In this programming assignment we will do a small experiment to see how C
    organizes arrays in memory.

    Write a program that initializes three arrays:

    - an array of 10 ints
    - an array of 10 floats
    - an array of 10 chars

    Now, for each array, write a program that prints the memory addresses of all 10
    entries of the array. It will be useful to use the printf conversion for
    unsigned integer, "%u".

    Finally, in your writeup, include some brief discussion about the numbers
    that you printed out and what they tell you about how C stores the array.

    4. Sorting. Submit your .c code file

    Write a sorting function to complete the code below. The code below creates
    a random array and passes it to the sort function you will write. Your code
    should put the entries in order from least to greatest.

    Feel free to use any sorting algorithm you like, as long as it works. Example
    output is shown below the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    /********************Write your code here********************/

    /*****************End of your code***************************/

    int main()
    float A[10], answer;
    int i;

    srand (time(NULL)); /* Seed the random number generator with the time */

    printf("The input array is:\n");
    for (i=0; i<10; i++) {
    /* rand() returns a random int. We want a random float, so divide by
    32767 */
    A = (float)rand()/32767.0;
    printf("%f ", A);
    printf("\n"); /* finish the line */


    printf("After sorting, the array is:\n");

    for (i=0; i<10; i++) {
    printf("%d ", A);

    return 0;

    /* End of code
    Example output:
    The input array is :
    0.943724 0.928800 0.752251 0.662069 0.194739 0.544939 0.824610 0.118503 0.070070 0.335917
    After sorting, the array is:
    0.070070 0.118503 0.194739 0.335917 0.544939 0.662069 0.752251 0.824610 0.928800 0.943724
  2. jcsd
  3. Nov 9, 2008 #2
    Last edited by a moderator: May 3, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook