SAMPLE TEST #2 Fall 2013 1. Portability of programs is one the important softwar
ID: 3683402 • Letter: S
Question
SAMPLE TEST #2
Fall 2013
1. Portability of programs is one the important software engineering goals. Different systems have different conventions regarding the implementation of basic data types. Modify function void
displayBits(unsigned), so that it is portable between systems using 2-byte, 4-byte, 8-byte, 16byte, 32-byte and larger unsigned integers (the function below is tailored for 2-byte - 16 bit unsigned integers).
HINT: Correct solution requires minimal code changes. You may not ask user to type in the size of an unsigned integer.
void displayBits(unsigned value)
{ unsigned c, displayMask; displayMask = 1 << 15;
printf("%u = ", value); for (c = 1; c <= 16; c++) {
putchar(value & displayMask ? '1' : '0'); value <<= 1; if (c % 8 == 0) putchar(' ');
}
putchar(' ');
}
2. Assume the following data structure is declared in the program. Also, assume that integers require 4 bytes of storage, floats require 8 bytes and each ASCII character is stored in 1 byte.
#include <stdio.h>
union number { int x; float y; char s[10];
};
main() {
union number val;
val.x = 5; printf("%d ", val.x); printf("%d ", val.y); val.y = 100.5; printf("%d ", val.y); printf("%s ", val.s);
scanf(“%s”, val.s); /* Assume user enters string ‘sometest’*/ printf("%s ", val.s);
return 0;
}
Please show what is printed out by each printf() statement in the program above and explain it shortly.
3. The following program opens two files and calls function modify_file2(inFilePtr, outFilePtr). You may assume that both files are text files, and that both files can be opened properly.
#include <stdio.h>
void modify_file2(FILE*, FILE*);
main(int argc, int *argv[])
{
FILE *inFilePtr, *outFilePtr; int c;
if (argc != 3)
printf("Usage: copy infile outfile "); else
if ((inFilePtr = fopen(argv[1], "r")) != NULL) if ((outFilePtr = fopen(argv[2], "w")) != NULL) modify_file2(inFilePtr, outFilePtr); else
printf("File "%s" could not be opened ", argv[2]); else
printf("File "%s" could not be opened ", argv[1]);
return 0;
}
void modify_file2(FILE *inPtr, FILE *outPtr)
{ int c; if ((c = fgetc(inPtr)) != EOF)
{modify_file2 (inPtr, outPtr); fputc(c, outPtr);
}
}
The file pointed by inFilePtr contains only the following line of text:
This is a test sentence in infile.
Upon the execution of the above program, determine the contents of the file pointed by outFilePtr.
Write a short C program which, when invoked by the name of its executable ‘multiply’ in the command line, followed by two or more operands, calculates the result of the corresponding mathematical operator and displays it on the screen. Your program should work correctly for any number of operands.
Example:
%multiply 8 3 2
%48
Assume you have built the binary search tree composed of the following elements:
Write a C function int search(TREENODEPTR tree_ptr, int n) which should determine whether n exists in the binary tree. If n exists, the function should print out the numbers encountered in the nodes during the search and return the number of visited nodes. For example, the function call search(root, 9) should cause the printout 24, 12, 8, 9, and the returned value should be 4 (24 and 12 and 8 and 9 have been visited in this search). In case of search(root, 28), the function should print out the visited nodes (24, 44, 29) and return -3, meaning the node with number 28 is not found (-) and 3 comparisons have been performed.
(HINT: Iterative and recursive solutions exist. As long as the program is correct, you will receive full points, regardless of the solution you choose).
struct treeNode { struct treeNode *leftPtr; int data;
struct treeNode *rightPtr;
};
typedef struct treeNode TREENODE; typedef TREENODE *TREENODEPTR;
int search(TREENODEPTR tree_ptr, int n)
{
Explanation / Answer
1.
void displayBits(unsigned value){
unsigned c, displayMask;
displayMask= 1 << (sizeof(unsigned)*8-1);
printf("%u = ",value);
for (c = 1;c <= sizeof(unsigned)*8;c++){
putchar(value & displayMask ? '1' : '0');
value <<= 1;
if (c % 8 == 0){
putchar(' ');
}
}
putchar(' ');
}
2.
output:
5
0
0
provide more info of other question. so that i can solve that
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.