Pointers, Arrays and Pointer Arithmetic - Laboratory 5 | CS 0449, Lab Reports of Computer Science

Material Type: Lab; Class: INTRO TO SYSTEMS SOFTWARE; Subject: Computer Science; University: University of Pittsburgh; Term: Spring 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 09/02/2009

koofers-user-zbs
koofers-user-zbs 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS449, Introduction to Systems Software, Spring 2007
TA: Ricardo Villamarín-Salomón
Lab #5
Exercise 1 Pointers, Arrays and pointer arithmetic
a) What does the program in Listing 1 do?
Listing 1
#include <stdio.h>
#define NUM_ELEMS 7
int main()
{
int arr[NUM_ELEMS] = {5, 8, 10, 2, 9, 4, 1};
int *p, *pend;
for (p=arr, pend=(p + NUM_ELEMS); p<pend; p++)
printf("%d\n", *p);
return 0;
}
b) Does the program in Listing 2 do the same as the one in Listing 1?
Run it and see if you can explain the difference. What about the program in Listing
3?
Listing 2
#include <stdio.h>
#define NUM_ELEMS 7
int main()
{
-1/5-
pf3
pf4
pf5

Partial preview of the text

Download Pointers, Arrays and Pointer Arithmetic - Laboratory 5 | CS 0449 and more Lab Reports Computer Science in PDF only on Docsity!

CS449, Introduction to Systems Software, Spring 2007

TA : Ricardo Villamarín-Salomón

Lab

Exercise 1 Pointers, Arrays and pointer arithmetic

a) What does the program in Listing 1 do?

Listing 1 #include <stdio.h> #define NUM_ELEMS 7 int main() { int arr[NUM_ELEMS] = {5, 8, 10, 2, 9, 4, 1}; int *p, *pend; for (p=arr, pend=(p + NUM_ELEMS); p<pend; p++) printf("%d\n", *p); return 0; }

b) Does the program in Listing 2 do the same as the one in Listing 1?

Run it and see if you can explain the difference. What about the program in Listing

Listing 2 #include <stdio.h> #define NUM_ELEMS 7 int main() {

short int arr[NUM_ELEMS] = {5, 8, 10, 2, 9, 4, 1}; int *p, *pend; for (p=arr, pend=(p + NUM_ELEMS); p<pend; p++) printf("%d\n", *p); return 0; } Listing 3 #include <stdio.h> #define NUM_ELEMS 7 int main() { double arr[NUM_ELEMS] = {5.0, 8.0, 10.0, 2.0, 9.0, 4.0, 1.0}; float *p, *pend; for (p=arr, pend=(p + NUM_ELEMS); p<pend; p++) printf("%d\n", *p); return 0; }

Exercise 2 Structs^1

a. What will be the result of attempting to compile and run the following

program?

#include <stdio.h> typedef struct{ int x; int y; }INTEGERS; void manipulateIntegers( INTEGERS ); void printIntegers( INTEGERS ); int main() { INTEGERS i; i.x = 2; i.y = 3; printIntegers( i ); manipulateIntegers( i ); printIntegers( i ); } void printIntegers( INTEGERS i ) (^1) By Mohammad Hammoud

STUDENT s[2]; char *arr[2][3]= { {"MHH1", "123", "pgh"}, {"MHH2", "456", "pgh"} }; enrollStudent(&s[0], arr[0]); enrollStudent(&s[1], arr[1]); printDatabase( s); } void printDatabase( const STUDENT s[] ) { printf( "NAME\tID\tADDRESS\n\n"); int i; for( i = 0; i < 2; i++) printf( "%s\t%s\t%s\n", s[i].name, s[i].id, s[i].address ); } void enrollStudent( STUDENT *s, char *data[]) { s->name = data[0]; s->id = data[1]; s->address = data[2]; }

Exercise 3 Recursive functions and Pointer arithmetic^2

(Palindromes) A palindrome is a string that is spelled the same way forward and

backward. Some examples of palindromes are "radar" and "civic".

a. Write a recursive function testPalindrome that returns 1 if the string passed as a

constant parameter is a palindrome and 0 otherwise. For this exercise, you can

assume that the string passed contains only letters. You may use Listing 4 as a

starting point if you want.

b. Write a function testPalindromePtr that behaves in the same way that testPalindrome

but instead of receiving an entire string, just receives two pointers, one pointing to

the first character and the other to the last character of the part of the string that is

still unprocessed (do not use the [ ] operator). Use Listing 5 as a starting point.

Listing 4 #include <stdio.h> #include <string.h> int testPalindrome(const char string, int start, int end) { / write your code here */ } int main() { printf ("%d\n", testPalindrome("radar", 0, strlen("radar")-1)); printf ("%d\n", testPalindrome("saas", 0, strlen("saas")-1)); (^2) Modified from exercise 6.31, pg. 265 in the book

printf ("%d\n", testPalindrome("scifi", 0, strlen("scifi")-1)); return 0; } Listing 5 #include <stdio.h> #include <string.h> int testPalindromePtr(char *start, char end) { / write your code here */ } int main() { char *string; int strlength; string = "civic"; strlength = strlen(string); printf ("%d\n", testPalindromePtr( &string[0], &string[strlength-1] )); string = "saas"; strlength = strlen(string); printf ("%d\n", testPalindromePtr( &string[0], &string[strlength-1] )); string = "none"; strlength = strlen(string); printf ("%d\n", testPalindromePtr( &string[0], &string[strlength-1] )); return 0; }