531 final Flashcards
(103 cards)
Output of the following program?
#incude <studio.h>
#include <stdib.h>
struct name {
int a;
};
int main(){
struct name *ptr;
int i,n=5;
ptr(struct name*)malloc(n*sizeof(struct name));
for(i=0;i<n; ++i{
(ptr+i)->a=n-i;
}
printf("%d\n", (ptr+3)->a);
free(ptr);
return 0;
}</stdib.h></studio.h>
2
using ascii chart what’s the output?
#incude <studio.h>
int main(){
char ch[6] = "CS531";
printf("%x", ch[2]);
return 0;
}</studio.h>
35 (%x is hexadecimal)
incude <studio.h></studio.h>
#include <stdib.h>
struct st{
int a[5];
char ch[10];
};
int main(void){
struct st obj;
struct st *stobj = &obj;
obj.a[3] = 7;
stobj->a[3] = 5;
strcpy(obj.ch, 'Midterm");
stobj->ch[1] = 32;
printf("\nobj.a[3]==%d obj.ch==%s \n", obj.a[3],obj.ch);
return 0;
}</stdib.h>
obj.a[3]== 5 obj.ch == M dterm
(32 is spacebar cuz dec in ascii)
output using ascii chart?
#incude <studio.h>
#include <stdib.h>
int main(int argc, char *argv[]){
if(argc ==1)
{
printf("Program name: %s\n", argv[0]);
}
else{
printf("%x\n"argv[1][2]);
}
return 0;
}</stdib.h></studio.h>
$ gcc MT c-o MT
$./MT CS531
35 (MT is argc and CS531 is argv, therefore you get [1][2] of argv which is 5 and then turn it into hexadecimal)
find output:
#incude <studio.h>
int main(){
char array[10] = "CS 531";
char *a_ptr = array;
int x=4;
printf("%s",a_ptr+x);
return 0;
}</studio.h>
31(a_ptr+x means it starts printing at 31 since x = 4)
time complexity for traversing a singly linked list?
O(N)
find output:
#incude <studio.h>
int main(){
int x[3][4] =
{
{110,161,302,453},
{11,16,30,45},
{1100,1600,3000,4500}
};
int y,z;
for{y=2;y>0;y--){
for(z=0;z<4;z+=2){
printf("$d\n",x[y][z]);
}
}
return 0;
}</studio.h>
1100,
3000,
11,
30
include <studio.h></studio.h>
int main(void){
int n=28,
lcv,
flag;
for(lcv=2, flag=1; lcv<=(n/2);lcv++){
if((n%lcv)==0){
if(flag)
printf(“%d \n”, n);
flag = 0;
printf(“%d\n”,lcv);
}
}
}
28
2
4
7
14
incude <studio.h></studio.h>
int main(){
int s=0,x,y,z=3;
for(x=0;x<z;x++)
for(y=x;y<z; y++)
s+= x*y;
printf(“%d\n”,s);
return 0;
}
7
get output, assume push pop and isEmpty as Stack functions (first in last out)
int main(){
int num1,quotient,rem;
num1 = 41;
quotient = num1;
while(qutient!=0){
rem = quotient%3;
quotient = quotient/3;
push(rem);
}
while(!isEmpty()){
printf(“%d”,pop());
}
return 0;
}
1112
using a stack data structure, the pop operationmay be categorized as having what computational time value?
O(1)
recursion base case
prove the statement true for some specific value or
values of n (usually 0 or 1).
induction step
assume the statement to be true for all positive
integers less than n, then use that fact to prove it true for n
BST depth
number of edges from the root to the node
BST height
number of edges from the node to its deepest leaf.
full binary tree
each node has either 0 or 2 children
complete bst
a binary tree which is completely filled with the possible exception of the bottom level which is filled left to right
preorder traversal
visit parent first then left and right children ( in that order)
inorder traversal
visit left child then parent and then the right child
postorder traversal
visit left child, then right child, and then parent
logarithms
each level has max 2^n amount of nodes. n being the height level from root.
BST vs Binary tree
Binary tree isn’t ordered. BST is.
each node contains one key
the keys in the left are less than the parent. the keys in the right are greater than the parent. no duplicates
deletion in BST
leaf nodes are trivial.
if the node being deleted only has one child then the node is replaced by the only child.
if the nose being deleted has two children, replace the deleted node with its inorder successor or inorder predecessor
pass by value vs pass by reference (int vs int *)
pass by value indicates a copy of the data that is passed to the function. any changes to the parameter have no affect on data in the calling function.
Pass by reference involves use of a reference parameter. it referes to the original data, therefore any changes made to the original data will be passed into the original variable.