Physics Forums

Physics Forums (http://www.physicsforums.com/index.php)
-   Engineering, Comp Sci, & Technology Homework (http://www.physicsforums.com/forumdisplay.php?f=158)
-   -   Can anyone help me with some basic programming homework? (http://www.physicsforums.com/showthread.php?t=270542)

wideris Nov9-08 11:21 AM

Can anyone help me with some basic programming homework?
 
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!!!!


HW:

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[i] = (float)rand()/32767.0;
printf("%f ", A[i]);
}
printf("\n"); /* finish the line */

mySort(A,10);

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

for (i=0; i<10; i++) {
printf("%d ", A[i]);
}
printf("\n");

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
*/

Nok1 Nov9-08 02:11 PM

Re: Can anyone help me with some basic programming homework?
 
http://en.wikibooks.org/wiki/Programming:C_contents is a good start. Especially about the bits, and initializing arrays question, along with the short answer questions.

For the sorting question, I recommend http://en.wikipedia.org/wiki/Sorting_algorithm, but since you are a beginner the easiest to implement is BubbleSort (http://en.wikipedia.org/wiki/Bubble_sort).

Come back when you have some concrete questions.


All times are GMT -5. The time now is 07:19 PM.

Powered by vBulletin Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2014 Physics Forums