Montana State University Bozeman C Programing Computer Science Task
A network address is made up of 4 sections – each an integer from 0-255. Each section is separated by a period (.) when shown to the user. Depending on the class of the address, one or more sections identify the network and one or more sections identify the host within the network. For an IP addresses from Class A, the first 8 bits (the first number) represent the network part, while the remaining 24 bits (last 3 numbers) represent the host part. For Class B, the first 16 bits (the first two numbers) represent the network part, while the remaining 16 bits (last 2 numbers) represent the host part. For Class C, the first 24 bits represent the network part (the first 3 numbers), while the remaining 8 bits (the last number) represent the host part. To idenfity which class a network is, we can look at the first number.
- A: 0-127
- B: 128-191
- C: 192-223
- D: 224-239
- E: 240-255
For this assignment, you will read in a file of network addresses (using scanf
and file redirection) and print out some statistics about the networks in the file. You should ignore Class D and Class E networks. Your program will also take in a command line argument indicating the number of network addresses to be processed.
The statistics you must compute are:
- The number of Class A, Class B, and Class C networks
- For each of Class A, Class B, and Class C, the network with the most hosts and the number of hosts it contains.
In order to help you develop your program, here is a rough idea of how you might structure your program.
- Convert the command line argument into an
int
. If there is no command line argument, print an error and end the program. This argument is the number of network addresses that will be in the file. - Read each address into a 2 dimensional array of 4 unsigned chars. The size of the first array dimension is the command line argument. (Note: you can use the
int
data type instead, but you will lose 5 points. If you feel more comfortable withint
, you could write your program withint
first and change it tounsigned char
after it is working.) - Sort the array. (You will need to implement a sorting algorithm of your choice.)
- Use the sorted array to compute the information you need.
Sample input and output
There are a number of sample input files in the /public/pgm1
directory.
Here is what your output should look like when you run your program on the inp17.txt
, inp2000.txt
, inp5000.txt
, and inp8000.txt
sample files.
<code>[program1]$ ./pg1 17 < inp17.txt Class A has 5 networks Largest A network is 106 with 3 hosts Class B has 5 networks Largest B network is 137.249 with 2 hosts Class C has 2 networks Largest C network is 215.116.26 with 2 hosts [program1]$ ./pg1 2000 < inp2000.txt Class A has 128 networks Largest A network is 38 with 16 hosts Class B has 484 networks Largest B network is 129.74 with 2 hosts Class C has 256 networks All C networks have only 1 host [program1]$ ./pg1 5000 < inp5000.txt Class A has 128 networks Largest A network is 95 with 35 hosts Class B has 1234 networks Largest B network is 144.40 with 3 hosts Class C has 640 networks All C networks have only 1 host [program1]$ ./pg1 8000 < inp8000.txt Class A has 128 networks Largest A network is 106 with 50 hosts Class B has 1961 networks Largest B network is 141.175 with 4 hosts Class C has 987 networks Largest C network is 216.24.49 with 2 hosts </code>
input files
inp10.txt, inp17.txt, inp2000.txt, inp500.txt, inp800.txt.i
inpa.txt
64.113.134.35
102.130.129.146
81.162.78.0
19.204.25.222
106.61.236.67
106.71.236.60
106.81.240.63
inpb.txt
168.12.110.25
137.249.183.201
168.14.111.27
168.17.111.27
137.249.111.202
137.246.111.202
inpc.txt
217.158.91.183
215.116.26.223
245.124.138.157
215.116.26.220
Hints
- If you prefer, you can write your entire program in one single file and just separate it into four different files before you turn it in.
- Print an
unsigned char
using the placeholder%hhu
. - Start early!
Requirements
- Write your program in a file called
program1.c
in yourcsci112-firstname-lastname/programs/program1/
directory. - You must use at least three functions in addition to
main
. Each should be stored in a separate file. (If you use more than four total functions, you can use four or more files.) - Since the network addresses are made up of integers between 0 and 255, use the
unsigned char
data type to store them instead ofint
. This way, they only take 1 byte (8 bits) instead of 4 bytes, meaning that your program will take only a quarter of the memory. - You must use a makefile to compile and link your separate files.
- You may not use global variables.
- Your output formatting must match the example. Use a tool like diffchecker to compare your output with the sample output. Don’t worry about trailing spaces.
- comments explaining what your program does
- code is indented so that it is readable
- there are no global variables
- compiles successfully with -Wall – no warnings
- successfully reads in and uses a command line parameter
- has error msg if no command line parameter
- reads all input into a 2 dimensional array of size command line number x 4
- the array is of
unsigned char
data type - compiles and links with makefile
- four functions are stored in different files
- computes the number of networks for class A, B, C correctly.
- identifies the largest networks for class A, B, C correctly, prints the largest network, and prints the correct largest count
- sorts addresses in order to count them
- correctly implements a sorting algorithm