1 Kigacage

Cpre 288 Homework Pass

Name: Lab Section:Question 4: C-Strings and pointers (10 pts) a.[5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access. Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.) // Append strt2 to str1 void my_strcat(char str1[], char str2[]) { //YOUR CODE HERE int i=0; int start_loc=0; // Find location to start appending to str1 (i.e. end of str1) while(str1[start_loc] != 0) { start_loc++; } // For each element in str2 append to str1 starting at // location start_loc in str1. while(str2[i] != 0) { str1[start_loc + i] = str2[i]; // Append str2 to str1 i++; } str1[start_loc + i] = ‘\0’; //Add NULL Byte to the end of str1 } // example of using my_strcat() #include <stdio.h> int main(void) { char my_str1[50] = “hello ”; char my_str2[] = “world”; my_strcat(my_str1, my_str2); // Printf should print: hello world.

Name : Lab Section: CprE 288 Fall 2014 – Homework 3 Due Fri. Sept 19 (on [email protected]) Notes:  Homework answers must be typed using a word editor.  Homework is individual work. Adhere to the University’s policy relating to the integrity of scholarship. See http://catalog.iastate.edu/academiclife/#regulationstext , “Academic Dishonesty”.  Late homework is accepted within three days from the due date. Late penalty is 10% per day (for each 24 hours following your class). Note: Unless otherwise specified, all problems assume the ATMega128 is being used Question 1: Function and Pointer (10 pts) The following function is intended to calculate the sum and product of two integers in a single function call: #include “stdio.h” // calculate the sum and product of two integers void sum_and_product (int sum, int product, int x) { sum = x + (x+1); product = x * (x+1); } // example of calling sum_and_product() void call_example() { int sum, product; int myarray[2] = {10, 20}; // compute 10 + 20 and 10*20 sum_and_product (sum, product, myarray[0]); printf(“sum = %d, product = %d”, sum, product); // Desired output of printf: sum = 30, product = 200 }

Leave a Comment


Your email address will not be published. Required fields are marked *