# introduction2programming theory (PDF)

### File information

Author: Ala

This PDF 1.5 document has been generated by MicrosoftÂ® Office Word 2007, and has been sent on pdf-archive.com on 05/11/2017 at 22:12, from IP address 176.221.x.x. The current document download page has been viewed 280 times.
File size: 281.21 KB (18 pages).
Privacy: public file

### File preview

2.1. What is the decimal value of the following expressions:
What is the decimal value of the following expressions? While evaluating the
value of the expressions take into account the previously executed
instructions.
{
int i, j, k, *p2i;
float fp;
p2i=&amp;k;

i=0x25; //2*16 + 5 = 37, 0x means that its hexadecimal,
j=025; //2*8 + 5 = 21, 0 means that its octal
k=25; //25
++k; //26
//k=26
k++; //26

//k is 27 here

k|7; //11011|00111 = 11111b = 31d,
but k doesn’t change
k&amp;011; //11011b &amp; 9d = 11011b &amp; 01001b = 01001b = 9d,
but k doesn’t change
k^0x2; //(xor, exclusive or)11011b ^ 00010b = 11001b = 25d,
but k doesn’t change

fp=10/20; //0
fp=(int) 10.0/20; //0
fp=(float) 10/20; //0.5

k+=2; //29
++(*p2i); //30

i&lt;&lt;3; //37d&lt;&lt;3 = 100101b &lt;&lt; 3 = 100101000b = 296d (poof - truth) //BAD-&gt;101000b = 40; cut only if &gt; 4bytes
i&gt;&gt;2; //37d&gt;&gt;2 = 100101b &gt;&gt; 2 = 001001b = 9d

2.2. Mark by '*' all instructions with compilation errors.

void syntaxErrors(void)
{
char *p1="12345";
char a0[]="12345";
char a1[1]="123";
//’char’ so single character in ‘’ or char code, no text
//isn’t enough to say that it
is too short?
char c;
p1=a0;
a0=p1;
a0==p1;
//not an error but it has no effect (just like `0;`)
p1=c;
//p1 is a pointer (an address) and c is a char
p1=&amp;c;
//here we pass the address to the char, cool
strcmp(c,a0);
//compare 1st char from a0[] to c without casting - warning/error
//c is a `char` and ought to be a `char *` in other words
strcmp(@c,p1);
//there is nothing like @ in C
a0=(char*) calloc(1,10);
//assigning a char* to char[]
p1=(char*) calloc(1,10);
free(a1);
//freeing cell of an array on a stack
free(c);
//trying to free value from stack
free(&amp;c);
//@up
return 'a';
// this would work, but the function is defined as void!
}

________________

2.3. Mark by '*' all instructions that lead to errors or to an
unpredictable program behavior at runtime.

char RunTimeErrorsAndProblems(void)
{
char *p1, c;
char ar[10];
char a1[31]="agsdfsdf";
puts(p1);
//p1 points to random memory
puts(a1);
if (p1==a1) puts("0");
//Doesn’t compare the strings if that’s what you meant // makes no sense since p1 has no
exact value
if (p1=a1) puts("1");
//Looks weird but no complaints from the compiler // an assignment, not comparison and
still p1 has no value //I think that assingment should be threated as “unpredictable program behavior”, cause it will
always execute.
p1=(char *) calloc(1,10);
if (p1==a1) puts("2");
//Doesn’t compare the strings if that’s what you meant //nothing bad just innocent

comparison//no to jak w końcu? mz jest co najmniej nielogicznie //compare string in a1 with some random shit that is
in just allocated p1! //doesn’t compare the strings but the adresses of the value pointed by the pointer and the adrress
of the first array element
strcmp(a1,ar);
//ar contains garbage // ar has no value
ar[0]='a';
ar[100]='a';
//Out of array bounds
p1=(char*) calloc(1,10);
//Memory leak, previously allocated memory wasn’t freed
free(++p1);
//No idea what happens here but itisn’t right //IMO moves pointer -1, then free, bad!
free(a1);
//Cannot free stack variables
free(&amp;c);
//Cannot free stack variables
return (*a1);
//Expecting to return a `char` got `char**` instead
}

________________

3. Define a structure type that describes a student
The StudentStructure structure type should contain the following data:
• name: a string of 20 characters;
• birthYear: a positive integer value;
• eyeColor: an enumerative type that contains basic colors;
• remarks: a string of characters without any fixed length limit.

Define the following variables:
• stStack - a structure filled with proper data and located on the stack
• stStack - a structure filled with proper data and located on the heap
• stStack - a structure filled with proper data and located in the global memory area
• a10 - an array of 10 such structures of that type
• pA10 - an array of 10 pointers to such structures
• aN – an array of n such structures, n is an integer variable;

typedef enum
{
RED,
GREEN,
BLUE
} EyeColor; //To be used in the Student struct

typedef struct
{
char name[20];
unsigned int dateOfBirth;
EyeColor eyeColor;

char* remarks;
} Student;

int main()
{
//Student on the stack (in the global memory area if outside all the functions)
Student student = { “Joe”, 1993, BLUE, “ ” };
//Student on the stack - alternative way
Student student = { .name=”Joe”, .dateOfBirth=1993, .eyeColor=BLUE, .remarks=”” };
//Student on the heap
Student *student = malloc(sizeof(Student));
strncpy(student-&gt;name, “Joe”, sizeof(student-&gt;name));
student-&gt;dateOfBirth = 1993;
student-&gt;eyeColor = BLUE;
student-&gt;remarks = strdup(“”);

Student a10[10];
//An array of 10 students,
int i;
for (i=0; i&lt;10; i++)
{
strncpy(a10[i].name,"Joe", sizeof(a10.name)); // andrzej always prefers using strncpy
a10[i].dateOfBirth=1993;
a10[i].eyeColor=BLUE;
a10[i].remarks="";
};

Student *pa10[10];

//An array of 10 pointers to students, fill it with data

for (i=0; i&lt;10; i++)
{
pa10[i] = malloc(sizeof(Student));
if(pa10[i]!=0)
{
strncpy(pa10[i]-&gt;name,"Joe",sizeof(pa10-&gt;name));
pa10[i]-&gt;dateOfBirth=1993;
pa10[i]-&gt;eyeColor=BLUE;
pa10[i]-&gt;remarks=strdup("");
}
}
//OR
for (i=0;i&lt;10; i++)

Student *aN = malloc(stud_count*sizeof(Student)); //An array of N students, do it, fill it

if(aN!=0)
{
for (i=0; i&lt;stud_count; i++)
{
strncpy(aN[i].name,"Joe",sizeof(aN[i].name));
aN[i].dateOfBirth=1993;
aN[i].eyeColor=BLUE;
aN[i].remarks=strdup("");
}
}

return 0; }
________________
4. Write a code snippet that:
1. Initializes an integer pointer to an integer variable located in the global memory
area.

int i = 10;

int main(int argc, char* argv)
{
int *pi = &amp;i;

}

2. Initializes an integer pointer to an integer variable located on the stack.

{
int *pi, i = 10;
pi = &amp;i;

}

3. Initializes an integer pointer to an integer variable located on the heap.

{
int *pi = malloc(sizeof(int));
...

}

4. Defines an enumerative type Color that includes back, white and yellow.

typedef enum {
BLACK,
WHITE,
YELLOW
} Color;
5. Defines a direct recursive function.

int factorial(int x)
{
return ( x == 1 ? 1 : factorial(x-1)*x ); //if x==1 return factorial(x-1)*x, else return 1; “ternary operator”
}

6. Defines an indirect recursive function.

void func1()
{
func2();
}

void func2()
{
func1();
}

7. Defines a void function with two integer parameters.
The value of the output parameter must be set to the value of the input parameter multiplied by 3

void multiplyByThree(int input, int *output)
{
*output = input*3;
}

8. Defines a function that tests if a number is the multiple of 17. If yes then its value
should be 1 and 0 otherwise.

int isMultipleOfSeventeen(int number)
{
return !(number % 17 == 0);
}

//f (number % 17 == 0) return 1;
//else return 0;

9. Defines a function that tests if a number is positive and odd. If yes then its value
should be 1 and 0 otherwise.

int isPositiveAndOdd(int number)
{
return (number &gt; 0 &amp;&amp; (number % 2==1) );
}

//if (number &gt; 0 &amp;&amp; (number % 2!=0) ) return 1;
//else return 0;

10. Copies a string of up to 10 characters from a stack variable to the heap.

#include &lt;string.h&gt;
char *string = strndup( stringOnStack, 10 );
________________

5. Code analysis
5.1. Which functions are not properly defined. Justify your

int * p1()
{
a int i1;
= return(&amp;i1);
}
i1 is out of scope here even though we have a pointer to where it used to be
in other words, returns a dangling pointer

int * p2()

{
static int i1;
return(&amp;i1);
}
this is correct since its static, it doesn\t vanisGandhih beyond the scope, so everything is fine

int globInt;
int * p3()
{
return(&amp;globInt);
}
seems legit

int p4() {
int result;
return(result);
}
returns garbage (result has no set value), fine otherwise

int *p5() {
return((int*) malloc(sizeof(int)));
}
doesn’t check if the memory was allocated succesfully, fine otherwise

________________

5.2. What output will produce the function testF?

int f1(int k) {
static int v=2;
v+=k;
return(v);
}

int f2(int k) {
int v=2;
v+=k;
return(v);
}

int f3(int k) {
return(++k);
}

int f4(int *k) {
return(++(*k));
}

void testF()
{
int k=2;
f1(k);
//static int v=2; v+=k;
printf("f1:\t%d\n",f1(k));
//v+=k; //print k
f2(k=2);
//int v=2 //v is a local variable, resets every time the function is called
printf("f2:\t%d\n",f2(k));
//v+=k; //print k
f3(k=2);
//k passed by copy (by value) here, doesn’t change in testF function
printf("f3:\t%d\n",f3(k));
//returned ++k and printed
k=2;
f4(&amp;k);
//passing by address (by pointer). Incrementing by one
printf("f4:\t%d\n",f4(&amp;k));
//incrementing once again and printing
}
0//The output:
f1:
f2:
f3:
f4:

6
4
3
4

//v is static, that’s why it holds it’s value between function calls!

1. What is an algorithm?
Algorithm is an effective method for solving a problem expressed as a finite sequence of well defined steps.
2. What is the native language of a computer?
Machine Code / code that is written to run on a specific processor
3. What is an assembler?
Program which translates from assembly language to machine language.
4. What are the basic differences between an assembler and a high level language?
Assembly language is a low-level language, you need to know the architecture of processor
In high-level languages the operating system will take care of the job of converting the basic program into the machine
code of that particular processor. High-level languages are nearer to human language.
5. Name the basic property of structural programming.
Divide and Conquer (divide long alogorithm into small problems)

introduction2programming-theory.pdf (PDF, 281.21 KB)

#### HTML Code

Copy the following HTML code to share your document on a Website or Blog