CS101 Prelab 12

Name: _________________________  

Netid: ___________________     Section: ____________

Print out this page and fill in the blanks to answer the following questions. Hand in this assignment at the beginning of Lab 12. You should use the material found in Lecture 19&20 and 21 of the course notes. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.


  1. Write the code to create a structure definition for jump, a structure with two integer data members(fields) called up and down. Use typedef to name the structure type jump.

    ______________________________


    ______________________________


    ______________________________


    ______________________________

    Define your structure in such a way that you can declare a strucure variable of type jump as follows:
    jump bungee;



  2. Now suppose we declare an array of jump structures as follows:   jump manyJumpers[10];


        (a) Write a single line of C code to assign the value 5 to the down member(field) of the second element of the array manyJumpers.
            
              _________________________________
        (b) Write a single line of C code to assign the value of the  down member(field) in the third element of the manyJumpers array to the down   
             member(field) of tenth element of the manyJumpers array.
           
              ________________________________

         (c) Write a single line of code to give the second element of the manyJumpers array the same values as the third element of manyJumpers array.

              ________________________________


3. In this problem, you are going to use functions from math library. You will need to know how to make eclipse able to compile functions in the math library. To do this perform the steps shown below: open eclipse, create a new project lab12 as managed c project, right click on lab12, on the property item, you will see this

then you select "C/C++ Build", in "Tool Settings" you select "GCC C Linker", then select "Libraries"



then click on "+" (find Libraries (-l) and look immediately to the right)


in "Enter Value", just enter "m"
  Click on "OK" to finish this setup, ready to deal with this problem 3:
Complete the code for the function named convert that converts points in polar coordinates to points in cartesian coodinates by means of the formula,

     x  =     radius *  cos(theta)
     y =      radius  *  sin(theta)

When your code is including in the following program you should obtain the output shown below.

#include  <stdio.h>
#include <math.h>

typedef struct {
    double x;
    double y;
}  cartesian;

typedef struct {
    double radius;
    double theta;
 } polar;

cartesian  convert( polar point)
{

    cartesian p;
   
   ___________________________________

  ___________________________________

  return p;
}
void main(void)
{
  polar point = { 1.0 , 3.141592653589793 / 4 };
  cartesian p ;
  p = convert(point);
  printf(" x = %lf ,  y = %lf \n", p.x , p.y);

which should display
    x = .707107        y = .707107



4.(Questions 4 and 5 use the following datatype named Student). Fill in the code needed to initialize the variable named bob with the information provided in myName,myNetid, and soc. Note that the datatype of the variable bob is Student.


    #include <stdio.h>

    typedef struct
    {
        char name[20];
        int ssn;
        char netid[20];
    } Student;

    void printStudent(Student);

    int main(void)
    {
        char myName[] = "Bob Smith";
        char myNetid[] = "bobsmith";
        int soc = 123456789;
        Student bob;


        strcpy( _________________, myName);


        strcpy( _________________, myNetid);


        ______________ = soc;

        printStudent(bob);

        return 0;
    }


5. Fill in the code for the printStudent function.

    void printStudent(Student foo)
    {
        printf("Name: ");


        printf(_______________, _______________); /* Print the name */
        printf("\n");
        printf("Netid: ");


        printf(_______________, _______________); /* Print the netid */
        printf("\n");
        printf("SSN: ");


        printf(_______________, _______________); /* Print the SSN */
        printf("\n");
    }

6. When using the conversion specifier %s, scanf() ignores all leading spaces and reads until the next space. However, scanf() will continue to read even if it goes past the end of a character array if no space is reached. Another problem with %s is that we cannot read more than one word at a time since the space character serves as a delimiter.  To fix this problem (only for reading strings) we will use the brackets conversion specifier %[  ], consult page 498 of Applied C for more information.
Inside the brackets we will use the special symbol '^' meaning NOT. For example, %[^;]; then the [^ ; ] part means read all characters (including blanks) while the character is NOT ; (and therefore stop reading when the next character is a ; ) and the ; outside the [^ ;]; means read and discard the next ;. For example, if we declared,

char artist[40];
char song_name[40];
char album_title[40];
int track_length;
Assume that the standard input (from the keyboard) contains:
The  Beatles;   214
Then
   scanf(" %[^;]; %i", artist, &track_length);
reads "The Beatles" into the array 'artist' and the number 214 into 'track_length'.

 Complete the following C program to read in

   The  Beatles; The Long and Winding Road; Let It Be... Naked; 214
into 'artist', 'song_name', 'album_title' and 'track_length' #include <stdio.h>
void main(void){
char artist[40];
char song_name[40];
char album_title[40];
int track_length;

scanf(" %[^;]; %____________ %_____________ %i",artist,

__________________, ____________________, _______________);

printf("%s  \n %s \n %s \n %i\n",artist,song_name,
album_title,track_length);
}