C Programming Assignment
I thought I would share this for those who are interested thumbs up
List the output of the following code and explain it without using the compiler.
#include <stdio.h>
int main () {
int x [5];
x[0] = 100; x[1] = 50; x[2] = 25; x[3] = 12; x[4] = 6;
int *y = x;
printf("%d\n", *y+2); printf("%d\n", *(y+2)); printf("%d\n", x[5]); printf("%d\n", y);
}
2.) Create a Swap Function so that it correctly swaps two Integers and has the following output. Also include the call to the function in main. You must use pointers in your solution.
1,0 0,1
#include<stdio.h>
int main(int argc, char ** argv) {
int x = 0; int y = 1;
printf("%d,%d\n", x,y);
// Swap Function
printf("%d,%d\n", x,y);
}
3.) Given the following Point Struct Create 5 points and chain them together using the next pointer, then print them out using a while loop.
Struct Point { int x; int y; point *next;
}
Ill post the answers later…..
1.)
printf("%d\n", *y+2); // 102 printf("%d\n", *(y+2)); //25 printf("%d\n", x[5]); //writes a random number out of system memory, as it is outside of what we implemented. printf("%d\n", y); // writes a random number out of system memory, as it is outside of what we implemented.
2.) Create a Swap Function so that it correctly swaps two Integers and has the following output. Also include the call to the function in main. You must use pointers in your solution.
1,0 0,1
#include<stdio.h>
int main(int argc, char ** argv) {
int x = 0; int y = 1;
void swap (int * _x, int * _Y);
printf("%d,%d\n", x,y);
swap(&x,&y);
printf("%d,%d\n", x,y);
} // swap function void swap (int * _x, * _y) {
int x = * _x; int *_x = *_y; int *_y = x;
}
3.) Given the following Point Struct Create 5 points and chain them together using the next pointer, then print them out using a while loop.
Struct Point { int x; int y; point *next;
}
int main ( int argc, char **, argv) {
position a; a.x = 100; a.y = 200;
position a2; a2.x = 1000; a2.y = 2000; a2.next = NULL; a.next = &a2;
position a3; a3.x = 10000; a3.y = 20000; a3.next = NULL; a2.next = &a3;
position a4; a4.x = 100000; a4.y = 200000; a4.next = NULL; a3.next = &a4;
position a5; a5.x = 1000000; a5.y = 2000000; a5.next = NULL; a4.next = &a5;
position aa = &a;
while (AA != NULL) {
printf("%d,%d\n", aa->x, aa->y); aa = aa->next;
}
}