C++ Programming and Introduction to Linux – Program 1

Hint: Copy and Paste answers from the command line.

1. Read C Programming Chapter 1. Enter date here when completed._________________

2. Boot up your Linux system.

3. Create a Terminal/Console window running the Bash command interpreter (shell). Type "help" to learn more.

4. Two of the editors are gedit and vi. Enter the following program using either editor in file hello.c.
You may need to google "Linux vi tutorial". Enter the dates completed.
vi Date__________________ gedit Date______________________
#include <stdio.h>
int main(int argc, char *argv[]) { printf("hello world\n"); return 0;}

5. Use pwd to print the current directory path. What is it? ___________________________

6. Type man mkdir. Type q to quit. Note vi is used for display.
What is displayed on the first line of output?____________________________________

7. Type mkdir. What is displayed on line 1 of output? _____________________________

8. Type mkdir --help. What is displayed on line 1 of output? ________________________

9. Create a directory named testX (where X is the last digit of your student id) using the mkdir command.

10. Use the mv command to move hello.c to directory testX.

11. Use the cd command to set the current directory to testX.

12. Type the ls -l command. What is the output ___________________________________

13. Type gcc --help. Gcc is the Gnu C language compiler. A compiler takes as input one or more .c (i.e test.c) files and outputs either assembly language (.s file), or an object file (.o) of machine instructions and a symbol table, or a packaged executable file (a.out).

14. What is the usage info for the -o option? _______________________________________

15. Type man gcc. Note that the help file is quite long. Note that vi commands (i.e. ^f ^b) can be used while the file is being displayed. Note that typing a q at the vi : prompt will terminate the help display.

16. Find one of gcc man pages on the Internet. What is the URL? ____________________________

17. Type gcc -c hello.c.
Once you have fixed all compile errors, type the ls command.
What new file did the compiler generate? _______________________________________

18. Type gcc -S hello.c. What new file did the compiler generate? _______________________
Type the cat command on the new file. What is line 1?_____________________________

19. Type gcc -E hello.c. What is line 1 of the output?_____________________________

20. Type gcc hello.c. What new file did the compiler generate? _______________________
The new file is the executable for the program. Type its name to execute it.
What did you type to make it execute and what is the output?______________________

21. Use >flip.txt to redirect the output to file flip.txt. List your command line._______________

22. Type od -c flip.txt. List the output._____________________________________________

23. Use the rm command to delete all files EXCEPT hello.c.

24. Type the set command. What is the 1st directory in the search PATH?___________________

25. Use cat | and sort on the command line to print the 2 lines of hello.c in int/# order.
What command line was successful? ___________________________________

26. Place an & at the end of the previous command line. What is the output? ________________

SUBMIT A COPY OF THIS DOCUMENT (with answers).

Program 2 (after Exercise 3)

Define a POWER4 macro function in a .h file named test.h then add a #include for test.h in your program as well as three test cases for POWER4. Attach a screen shot that lists your program and its output.

P (after Exercise 6)

· Look up the documentation for glBegin. Write an OpenGL program to draw figures using five of the ten possible glBegin options (GL_POINTS etc). Attach a listing of your program and a screen shot of its output.

· Note that the style of glBegin/glEnd is very CPU intensive as the processor is "pushing" vertex and color data to the graphics chip. More recently, especially on cell phones, the emphasis has changed to where the graphics chip "pulls" data from memory. This design change allows the CPU and graphics chip to execute in parallel. The trade-off is that programs use more memory to store vertices rather than calculating them on-the-fly.

Program 4

The String args convention for the static main method in Java derives from the argc/argv convention in UNIX/C. Java does not need argc because Java arrays have a length property. C arrays do not have a length property so C methods that manipulate arrays must ALWAYS pass an integer length and the array. The argv array is a vector of string references. By convention, argv[0] is the path of the executable e.g. sum dog cat results in argc=3 argv[0]="/usr/x/sum" argv[1]="dog" argv[2]="cat". By convention, the UNIX shell expands all wild cards so sum dog* would not have argc=2. Windows, on the other hand, does not expand wild card symbols so argc would be equal to two.

1. Start a Linux command shell (Terminal) window.

2. Implement a UNIX sum command. The atoi method can be used to convert an argv[i] string to an integer. Test your program with the following.
sum result should be 0
sum 5 6 7 result should be 18
sum -1 -2 -3 -4 result should be -10

3. The program is not very general purpose. What if we wish to sum a lot of numbers? It is more likely that they would be in a file. Extend the program so that if argc=1, it reads the numbers (using scanf) from stdin until an end-of-file occurs.
Enter the following data exactly as shown in a file named test.txt.
0
5 6 7
-1 -2 -3 -4
sum <test.txt result should be 8

4. The atoi function returns 0 if the argument is composed of letters. Extend the program so that if atoi returns 0, the corresponding argv is treated as a file name. To read a named file, use the fopen, fscanf, and fclose methods. Test your program with the following command line.
sum 5 test.txt 6 test.txt 7 result should be 34

5. Turn in a listing of your final program and a screen shot of running the step 4 command.

Program 5

Unlike Java, C and C++ arrays are not objects; they are simply replicated primitive types. Thus, there is no "new" required, you just declare how many elements the program needs. If an array size is determined at runtime, the array can be dynamically allocated. int x[10]; declares an integer array of ten elements; the subscripts range from 0 to 9.

Arrays can be statically (at compile time) initialized as follows:
int a[]={6,7,8,9}; // 4 elements
int b[4]={6,7,8,9}; // also 4 elements

Typically #defines, such as MAX_SIZE, are used to parameterize a program's array sizes. There is no length function as in Java but the number of elements can be computed as sizeof(b)/sizeof(type of array i.e. int).

1. Start a Linux command shell (Terminal) window.

2. Write a C program to count the number of each unique integer in its input stream. The program should be named "count". You can assume that the numbers are read from standard input.
count
3 5 1 3 8 7 2
2 3 7 1
1 1 1 1
ctrl-D
EXPECTED OUTPUT
3 3
5 1
1 6
8 1
7 2
2 2

3. Modify the program to sort the unique integers on output into ascending order. You can assume that there are no more than 1000 unique integers. Test your program with the following data. Turn in a screen shot of the program's output and a code listing.
9 1 13 2 14 4 3 3 9 10 2 2 3 12 1 8 2 13 4 11 6 4 1 3 1
12 7 8 1 4 8 1 6 3 3 10 8 2 2 4
5 2 7 5 6 5 16 9 6 10 1 13 12 15 4 13 16 9 2 6 19 10 3 4 14 6 17 9 4 18
10 6 12 15 5 11 1 5 9 12 11 7 8 20 5 2 18 7 4 8 1 19 3 15 5 15 11 8 5
17 8 17 3 3 2 2 2 6 10 7 10 1 6 11 14 10 17 4 2 16 8 13 14 11 12 3 16 15 12 9 16 4 13 6 9 4 1 5 2 6
8 7 5 1 5 1 11 1 9 10 5
12 14 8 3 1 9 3 11 4 6 6 2 9 12 6 8 3 15 13 3 3 10 5 5 6
1 3 10 13 4 4 11 14 1 7 8 7 7 7 2 1 2 1 1 7 2 4 11 14 18 7 3 5 7 7 2 9 4 5

Program 6

Write a C program to count the number of blank-delimited, unique strings in its input stream. The program should be named "counts". You can assume that the strings are read from standard input using scanf and the %s format.
#include <stdlib.h>
#include <stdio.h>
typedef char STRING[20]; //define the name of a new data type

int main(int argc, char *argv[]) {
STRING s;
while (scanf("%s", s)==1) {
printf("%s\n", s);
}
exit(0);
}
counts
cat bat dog cat sat hog bog
bog cat hog dog
dog dog dog dog
ctrl-D
EXPECTED OUTPUT
cat 3
bat 1
dog 6
sat 1
hog 2
bog 2

Program 7

C procedures are very similar to Java procedures. The primitive types (char int float long double) are all passed by value just like Java. Arrays are passed by reference just like Java; any change to a parameter changes the argument. Typically, methods MUST be declared before use. Otherwise, the method signature must be declared outside of any procedure AND before any reference to the method. See below. The "static" prefix in C designates a method as private to the file in which it is declared.

float sqrt(float x);

1. Implement the following method in both C and in C++ using the string class.
void sort(STRING x[], int y[], int n, int ascending);
The method sorts both x and y arrays into ascending order based on the string values in array x. If ascending is zero, both arrays are sorted into descending order. Test your method with the following code.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef char STRING[20];
STRING test[20];
int test2[]={1,2,3,4,5};
int main(int argc, char *argv[]) {
int i ;
strcpy(test[0],"bug"); strcpy(test[1],"aug");
strcpy(test[2],"dug"); strcpy(test[3],"cug");
strcpy(test[4],"mug");
sort(test, test2, 5, 1);
for (i=0; i<5; i++) printf("%s %d\n",test[i], test2[i]);
sort(test, test2, 5, 0);
for (i=0; i<5; i++) printf("%s %d\n",test[i], test2[i]);
exit(0);
}
OUTPUT
aug 2
bug 1
cug 4
dug 3
mug 5
mug 5
dug 3
cug 4
bug 1
aug 2

2. Turn in a listing.

Program 8

Imagine a Java class with only public data declarations and no methods or constructor. A structure declaration defines a new data type that is a composition of other data declarations. Thus, the previous program uses two arrays, STRING and int. These can be composed into a new data type using a structure (struct) declaration.
typedef char STRING[20];
typedef struct _record {
STRING buffer[1000];
int count[1000];
} Record;

As a side effect of the historical evolution of C, the declaration defines two new type names: _record and Record. Either of these names can be used as a simple variable type, an array type, or as a member of another structure declaration. The only oddity is that _record must always be preceded by the struct keyword i.e. struct _record x. Note that records (structs) are the cornerstone of database technology and are probably the most frequently used data type. Structure fields are referenced identically to public variables in Java classes with a qualified reference (x.count).

Unlike arrays, structures are passed to procedures by value (changes to parameters have no effect on arguments). As structures are usually many bytes in size, passing by value is undesirable. The alternative is passing structures by reference (changes to parameters change arguments). In Java, references are the only option and require no additional notation. In C, the * symbol indicates an explicit reference. As with Java, assigning one reference to another results in two references to the same structure. To un-reference a structure reference, the name must be preceded by an * ( (*x).count ). C defines an easier shorthand for structure references (the arrow notation ->). The following code illustrates structure parameter passing and references.
void test(Record p) {
p.count[2]=7;
}
void test1(Record *p) {
(*p).count[2]=7;
}
void test2(Record *p) {
p->count[2]=8;
}
int main () {
struct _record r;
Record s;
r.count[0]=5;
s.count[2]=6;
printf("%d %d\n", r.count[0], s.count[2]);
test(s);
printf("%d %d\n", r.count[0], s.count[2]);
test1(&s);
printf("%d %d\n", r.count[0], s.count[2]);
test2(&s);
printf("%d %d\n", r.count[0], s.count[2]);
return 0;
}

1. Rather than declaring a structure of two arrays, it would be much more sensible to declare a counted-name structure and then to use it in an array.
typedef char STRING[20];
typedef struct _record {
STRING name;
int count;
} Record;
Record unique[1000]={"bug",1,"aug",2,"dug",3,"cug",4,"mug",5};
Structure assignment can be implemented as follows:
memcpy(&unique[3], &unique[5], sizeof(Record)); //& means address of; creates a reference

2. Implement the following method.
void sort(Record x[], int n, int ascending);
The method sorts x into ascending order based on the string values in array x. If ascending is zero, x is sorted into descending order. Test your method with the following code. Turn in the screen shot of the output and a listing of your program.
int main(int argc, char *argv[]) {
int i ;
sort(unique, 5, 1);
for (i=0; i<5; i++) printf("%s %d\n", unique[i].name, unique[i].count);
sort(unique, 5, 0);
for (i=0; i<5; i++) printf("%s %d\n", unique[i].name, unique[i].count);
exit(0);
}
OUTPUT
aug 2
bug 1
cug 4
dug 3
mug 5
mug 5
dug 3
cug 4
bug 1
aug 2

Program 9

1. Implement the following code, which is not a class, and a test program that tests every method. Call push 5 times, once with each of the last 5 digits in your student id. Turn in a listing and screen shot of the output.
#define MAX 50
int stack[MAX], top;
void init();
void push(int x);
int pop();
void toString(char output[], int n); //top,stack[0],...,stack[top-1]
int parse(char input[], int n);