Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: From the textbook. Chapter 11 EGN 3211: Engineering Analysis and Computa

ID: 3767975 • Letter: P

Question

Part 1: From the textbook.

Chapter 11

EGN 3211: Engineering Analysis and Computation (Fall 2015)

Homework # 7

1. Identify and correct the errors, if there are any, in the following program segments. If there are errors,

briefly explain them.

(a)int *number; printf("%d ", *number);

(b) float *realPtr; int *integerPtr;

(c) int *x, y; x = y;

(d) char s[] = "this is a character array"; for( ; *s != ’’; s++)
printf("%c ", *s);

2. For each of the following, write a single statement that performs the indicated task. Assume that variables value1 and value2 of type long long int have been declared, and that value1 has been initialized to 100,000.
(a) Declare the variable llptr to be a pointer to an object of type long long int.

(b) Assign the address of variable value1 to pointer variable llptr. (c) Print the value of the object pointed to by llptr.

3. Give the output of the following program. #include <stdio.h>
#include <stdlib.h>
#define M1 2

}

4. What is the output of the following program? /* File: chars.c */
#include <stdio.h>

return 0; }

5. Given the following declarations and assignments, what do these expressions evaluate to? int a1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int *p1, *p2;

(a) *(a1+4) (b) a1[3] (c) *p1 (d) *(p1+5) (e) p1[-2] (f) *(a1+2) (g) a1[6] (h) *p2 (i) *(p2+3) (j) p2[-1]

Chapters 12 & 13:

6. Write a program that reads four strings and prints only those strings beginning with the letter ’b’.

7. Consider the following code segment:

what is wrong with the third statement?

rewrite the code correctly, you can use the string manipulation /conversion functions and new

variables.( just 3-4 lines code , do not write a program).

8. Write a program where the user inputs a six letter string. The program displays the six letter string onto the screen backwards. You can use pointer array of characters and pointer to character, you need for -loops and use getchar() and putchar() function for reading and writing characters.

Output example:

Please input a six-letter word

abcdef fedcba

9. Write a program that uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less than, equal to, or greater than the second string7. Write a program that uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less than, equal to, or greater than the second string

10. Write a program that inputs four strings that represent floating-point values, converts the strings to double values, sums the values, and prints the total of the four values. Input the strings "2", "10.5", "12", "12.5" to test your program.

11. State whether the following statements are true or false. If false, briefly explain why. (a) Members of a structure must have the same type.
(b) Structures cannot be compared using operator == or !=.
(c) Members of different structures can use the same name.

(d) It is not necessary to have a tag name for a structure.
(e) Call by reference is the only way to pass structures to functions.

12. Write a program using a structure that allows the user to enter the information of company name, address, phone number, and credit rating (good or bad) for a customer. At the end, the program displays the input from the user (use 32 bytes for each member).

13. Write a program cylinder.c that accomplishes the following:
Read a list of radii and heights from a file(cylinder_input.data). For each pair of values, calculate the cylinder's volume and surface area. Display the results on the screen and save the results to a file.

File: cylinder_input.data 1.5 10.2
2.21 20
3 30.243

55.23 2.2 12.1 45.989

The contents of the output file “cylinder_output.data” 1.500000 10.200000 72.099551 110.269902
2.210000 20.000000 306.877054 308.404496 3.000000 30.243000 855.100680 626.615787 55.230000 2.200000 21082.525775 19929.377237 12.100000 45.989000 21153.127133 4416.305811

Part 2: From Microcontrollers Vs Microprocessors: (see slide 57 & 58 for more details)
1. Write a code that makes the MSP430 blink the red LED and the green LED at the same time. The two

LEDs should be on at the same time and then off at the same time

2. Write a code that makes the MSP430 blink the red LED and the green LED in an alternating way -When the red LED is on, the green LED should be off
-Inversely, when the green LED is on, the red should be off
You can use the same delay as in the basic code for LED blinking- lecture note: 11-MicrocontrollersVsMicroprocessors.pdf

Do not use __delay_cycles() function.

Explanation / Answer

(a)int *number; printf("%d ", *number); --> correct but nort good approch because it will print any garbage value

(b) float *realPtr; int *integerPtr; integerPtr = realPtr; ---->Warning incompaitable assignment because one is Int type and other is Float Type.

(c) int *x, y; x = y;---> Warning because pointer is used to store address of varriable

(d) char s[] = "this is a character array"; for( ; *s != ’’; s++)
printf("%c ", *s);--- >incorrect as S is name of array you can not do S++ with array name.

correc approach->

char s[] = "this is a character array";

char *p=s;

for( ; *p != ’’; p++)
printf("%c ", *p);

2:

long long int value1;
    long long int value2;
    value1=100000;
    long long int *llptr;
    llptr=&value1;
    printf("%d",*llptr);

Q3: Output

a1[1][2]=6.000000

pa1[1][1]=5.000000

q4

1 n

2 n

3 n

4 n

5 n

6 n

7 n

8 n

9 klmno

10 klmno


q5:

p1 = a1+3; --> a1 is pointer to first element of array and a1+3 points to 4th element
     p2 = &a1[2]; ---> p2 stores the address of third element of array

(a) *(a1+4) (b) a1[3] (c) *p1 (d) *(p1+5) (e) p1[-2] (f) *(a1+2) (g) a1[6] (h) *p2 (i) *(p2+3) (j) p2[-1]

a). gives value at index 4 of array

b). give value at index 3

c). give value at index 3

d).give value at index 8

e).give value at index 1
f). give value at index 2
g).give value at index 6
h).give value at index 2
i).give value at index 5
j).give value at index 1

q6:
#include <stdio.h>

int main()
{
char *s[4];
int i;

for(i=0;i<4;i++)
{
   s[i]=malloc(sizeof(char) * 5);
printf("Enter String ");
scanf("%s",s[i]);
}
for(i=0;i<4;i++)
{

if(*s[i]=='b')
{
printf("%s ",s[i]);
}

}


system("PAUSE");  
return 0;
}


q7: char* str3 = str1 + str2; ==> invalid because '+' operator is not valid for char array in C/C++. you can use strcat(str1,str2);
q8 :
#include<stdio.h>
int main(){
    char str[50];
    char rev[50];
    char *ptr = str;
    char *ptr1 = rev;
    int i=-1;

    printf("Enter any string : ");
    scanf("%s",str);

    while(*ptr){
     ptr++;
     i++;
    }

    while(i>=0){
     ptr--;
     *ptr1 = *ptr;
     ptr1++;
     --i;
    }

    *ptr1='';

    printf("Reverse of string is : %s",rev);

    return 0;
}

q9:
#include <stdio.h>
#include <string.h>

int main ()
{
   char str1[15]="abcdef";
   char str2[15]="ABCDEF";
   int result;


   result = strcmp(str1, str2);

   if(result < 0)
   {
      printf("str1 is less than str2");
   }
   else if(result > 0)
   {
      printf("str2 is less than str1");
   }
   else
   {
      printf("str1 is equal to str2");
   }

   return(0);
}

Q10:
#include <stdio.h>
#include<stdlib.h>
int main()
{
    double a,b,c,d;
char str1[] = "2";
char str2[] = "10.5";
char str4[] = "12";
char str3[] = "12.5";
a = atof (str1);
b = atof (str2);
c = atof (str3);
d = atof (str4);
double total=a+b+c+d;
printf("%f",total);

system("PAUSE");  
return 0;
}

Q11:(a) Members of a structure must have the same type.---> No structure can have different data types varibles
(b) Structures cannot be compared using operator == or !=.--> correct because structure is collection of different types varriables
(c) Members of different structures can use the same name. ---> Yes because to access every member we use object of that structure.