C++ Programming in Xcode

 
These examples were prepared for the C Programming Language for a PC, but with some modifications, will work on a Mac using the Xcode tools.
Open Xcode and choose File --> New Project --> Carbon C++ Application.  Choose a suitable name for the project and save to your project folder.

Double-click on main.cp

In the main.cp window that opens highlight and delete everything below the green comments section.

 

You will be replacing it with the following:

 


Project 1:

//Copyright __MyCompanyName__2008. All rights reserved.

//

#include <stdio.h>

 int main()

{

   printf("John Q. Doe\n");

   return 0;

}

/* Result of execution

John Q. Doe

 */

You will notice that the text you type will take on various colours, depending on the function of each part.  This is a feature of XCode.  The /* symbols indicate a comment. Everything enclosed within /*  */ is hidden when you run the program.  Comments are included in programs to help to explain or remind you of what the program code is doing.

When you have entered everything exactly as shown, click on Build --> Build and Run


This is the program window. It is a simple program, but if you see this window, it works!


On newer versions of Xcode you may have to go to Run on the top menu bar and choose Console to see the program run.

There are 29 projects in this exercise.  Work through in sequence to complete them all. Save each with a different name so you don't accidentally write over top of any.  Hand in the completed projects to the Drop Box.


Project 2:

#include <stdio.h>

int main()

{

   printf("John Q. Doe\n");

   printf("1234 Main Street\n");

   printf("(505) 555-1212\n");

    return 0;

}

 /* Result of execution

 John Q. Doe

1234 Main Street

(505) 555-1212

*/


Project 3:

#include <stdio.h>

 int main()

{

int index;

 /* index = 13; */

   printf("The value of the index is %d\n", index);

   index = 27;

   printf("The value of the index is %d\n", index);

   index = 10;

   printf("The value of the index is %d\n", index);

    return 0;

}


Project 4:

#include <stdio.h>

int main()

{

int index;

   index = 13;

   printf("The value of the index is %d\n", index);

   index = 27;

   printf("The value of the index is %d\n", index);

   index = 10;

   printf("The value of the index is %d\n", index);

  

   printf("Index is %d\n it still is %d\n it is %d",

                                  index, index, index);

    return 0;

}


Project 5:

#include <stdio.h> 

int main()

{

int index;

   for(index = 0 ; index < 10 ; index = index + 1)

   printf("John Q. Doe\n");

   return 0;

}

 /* Result of execution

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

 */


Project 6:

#include <stdio.h>

int main()

{

int index;

 index = 0;

   while (index < 10)

   {

      printf("John Q. Doe\n");

      index = index + 1;

}

return 0;

}

/* Result of execution

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

 */


Project 7:

#include <stdio.h>

 int main()

{

int index;

    index = 0;

   do

   {

      printf("John Q. Doe\n");

      index = index + 1;

   } while (index < 10);

    return 0;

}

 /* Result of execution

 John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

 */


Project 8:

#include <stdio.h>

 int main()

{

int index;

    index = 1;

   do

   {

      printf("The count is now %2d",index);

      if (index == 3)

         printf(" and is equal to three.");

      if (index == 7)

         printf(" and is equal to seven.");

      printf("\n");

      index = index + 1;

   } while (index < 11);

    return 0;

}

 /* Result of execution

The count is now  1

The count is now  2

The count is now  3 and is equal to three.

The count is now  4

The count is now  5

The count is now  6

The count is now  7 and is equal to seven.

The count is now  8

The count is now  9

The count is now  10

 */


Project 9:

#include <stdio.h>

int main()

{

int index, square;

   for(index = 1 ; index < 13 ; index = index + 1)

   {

      square = index * index;

      printf("%5d%5d\n", index, square);

   }

    return 0;

}

 /* Result of execution

    1    1

    2    4

    3    9

    4   16

    5   25

    6   36

    7   49

    8   64

    9   81

   10  100

   11  121

   12  144

*/


Project 10:

#include <stdio.h>

 int main()

{

int index;

float inversion;

    for(index = 1 ; index < 13 ; index = index + 1)

   {

      inversion = 1.0/index;

      printf("%5d%9.5f\n", index, inversion);

   }

    return 0;

}

  /* Result of execution

     1    1.00000

    2     .50000

    3     .33333

    4     .25000

    5     .20000

    6     .16667

    7     .14286

    8     .12500

    9     .11111

   10     .10000

   11     .09091

   12     .08333

 */


Project 11:

#include <stdio.h>

 int main()

{

int index;

    for(index = 1 ; index < 101 ; index = index + 1)

   {

      if ((index >= 32) && (index <= 39))

         printf("%5d\n", index);

   }

   return 0;

}

 

/* Result of execution

   32

   33

   34

   35

   36

   37

   38

   39

 */


Project 12:

/*************************************************************/

/*                                                              */

/*     This is a temperature conversion program written in      */

/*      the C programming language. This program generates      */

/*      and displays a table of farenheit and centigrade        */

/*      temperatures, and lists the freezing and boiling        */

/*      of water.                                               */

/*                                                              */

/*************************************************************/

#include "stdio.h"

int tempcalc(itn centtemp);

int main()

{

int count;        /* a loop control variable               */

int farenheit;    /* the temperature in farenheit degrees  */

int centigrade;   /* the temperature in centigrade degrees */

printf("Centigrade to Farenheit temperature table\n\n");

    for(count = -2 ; count <= 12 ; count = count + 1)

   {

      centigrade = 10 * count;

      farenheit = tempcalc(centigrade);

      printf("  C =%4d   F =%4d  ", centigrade, farenheit);

      if (centigrade == 0)

         printf(" Freezing point of water");

      if (centigrade == 100)

         printf(" Boiling point of water");

      printf("\n");

   } /* end of for loop */

return 0;

}

 int tempcalc(int centtemp)

{

int faren;

faren = 32 + (centtemp * 9)/5;

   return (faren);

}

/* Result of execution

Centigrade to Farenheit temperature table

   C = -20   F =  -4

  C = -10   F =  14

  C =   0   F =  32   Freezing point of water

  C =  10   F =  50

  C =  20   F =  68

  C =  30   F =  86

  C =  40   F = 104

  C =  50   F = 122

  C =  60   F = 140

  C =  70   F = 158

  C =  80   F = 176

  C =  90   F = 194

  C = 100   F = 212   Boiling point of water

  C = 110   F = 230

  C = 120   F = 248

 */


Project 13:

#include "stdio.h"

 void writename();

 void writename()

{

   printf("John Q. Doe\n");

}

 int main()

{

int index;

    for(index = 0 ; index < 10 ; index = index + 1)

      writename();

    return 0;

}

/* Result of execution

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

John Q. Doe

 */


Project 14:

                           /* Chapter 5 - Program 1 - SUMSQRES.C */

                             /* Chapter 5 - Programming exercise 3 */

#include <stdio.h>

void header(void);

void square(int number);

void ending(void);

int sum; /* This is a global variable */

int main(void)

{

int index;

   header();          /* This calls the function named header */

   for (index = 1 ; index <= 7 ; index++)

      square(index);  /* This calls the square function */

   ending();          /* This calls the ending function */

   return 0;

}

 void header(void)     /* This is the function named header */

{

   sum = 0;     /* Initialize the variable "sum" */

printf("This is the header for the square program\n\n");

}

 void square(int number)   /* This is the square function */

{

int numsq;

   numsq = number * number;  /* This produces the square */

   sum += numsq;

   printf("The square of %d is %d\n", number, numsq);

}

void ending(void)   /* This is the ending function */

{

   printf("\nThe sum of the squares is %d\n", sum);

}

 /* Result of execution

This is the header for the square program

The square of 1 is 1

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

The square of 5 is 25

The square of 6 is 36

The square of 7 is 49

The sum of the squares is 140

 */


Project 15:

#include <stdio.h>

#define START  7

#define END   -5

 int main()

{

int index;

   for(index = START ; index >= END ; index = index - 1)

   printf("The value of the count is now %2d\n", index);

   return 0;

}

/* Result of execution

The value of the count is now  7

The value of the count is now  6

The value of the count is now  5

The value of the count is now  4

The value of the count is now  3

The value of the count is now  2

The value of the count is now  1

The value of the count is now  0

The value of the count is now -1

The value of the count is now -2

The value of the count is now -3

The value of the count is now -4

The value of the count is now -5

*/


Project 16:

#include "stdio.h"

#include "string.h"

int main()

{

int index;

char string1[6], string2[6], string3[6], all_three[18];

   strcpy(string1, "one");

   strcpy(string2, "two");

   strcpy(string3, "three");

   strcpy(all_three, string1);

   strcat(all_three, " ");

   strcat(all_three, string2);

   strcat(all_three, " ");

   strcat(all_three, string3);

   for(index = 0 ; index < 10 ; index = index + 1)

      printf("The final string is ---> %s\n", all_three);

   return 0;

}

/* Result of execution

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

The final string is ---> one two three

 */


Project 17:

#include <stdio.h>

int main()

{

int index, array1[10], array2[10], arrays[10];

  for(index = 0 ; index < 10 ; index = index + 1)

   {

      array1[index] = 2 + 2 * index;

      array2[index] = 10 * (index + 1);

   }

   for(index = 0 ; index < 10 ; index = index + 1)

      arrays[index] = array1[index] + array2[index];

for(index = 0 ; index < 10 ; index = index + 1)

      printf("%4d %4d + %4d = %4d\n", (index + 1), array1[index],

               array2[index], arrays[index]);

  return 0;

}

 /* Result of execution

   1    2 +   10 =   12

   2    4 +   20 =   24

   3    6 +   30 =   36

   4    8 +   40 =   48

   5   10 +   50 =   60

   6   12 +   60 =   72

   7   14 +   70 =   84

   8   16 +   80 =   96

   9   18 +   90 =  108

  10   20 +  100 =  120

 */


Project 18:

#include <stdio.h>

#include <string.h>

 char my_string[20] = "C is neat!";

int main()

{

int index;

  printf("%s\n", my_string);

   for (index = 0 ; my_string[index] ; index = index + 1)

   printf("%c", my_string[index]);

   printf("\n");

   for(index = strlen(my_string) ; index > 0 ; index = index - 1)

    printf("%c", my_string[index - 1]);

   printf("\n");

 return 0;

}

/* Result of execution

 C is neat!

C is neat!

!taen si C

 */


Project 19:

#include <stdio.h>

#include <string.h>

int main()

{

int index;

char stuff[20], *pt;

   strcpy(stuff, "This is a neat test.");

   pt = stuff;

   for(index = 0 ; index < 20 ; index++)

   {

      printf("A character is ---> %c\n", *pt);

      pt++;

   }

return 0;

}

/* Result of execution

A character is ---> T

A character is ---> h

A character is ---> i

A character is ---> s

A character is --->

A character is ---> i

A character is ---> s

A character is --->

A character is ---> a

A character is --->

A character is ---> n

A character is ---> e

A character is ---> a

A character is ---> t

A character is --->

A character is ---> t

A character is ---> e

A character is ---> s

A character is ---> t

A character is ---> .

 */


Project 20:

#include <stdio.h>

#include <string.h>

int main()

{

int index;

char stuff[20], *pt;

   strcpy(stuff, "This is a neat test.");

   pt = stuff + 19;

   for(index = 0 ; index < 20 ; index++)

   {

      printf("A character is ---> %c\n", *pt);

      pt--;

   }

  return 0;

}

  /* Result of execution

A character is ---> .

A character is ---> t

A character is ---> s

A character is ---> e

A character is ---> t

A character is --->

A character is ---> t

A character is ---> a

A character is ---> e

A character is ---> n

A character is --->

A character is ---> a

A character is --->

A character is ---> s

A character is ---> i

A character is --->

A character is ---> s

A character is ---> i

A character is ---> h

A character is ---> T

*/


Project 21:

#include <stdio.h>

#include <conio.h>

 int main()

{

char input_char;

    printf("Hit any key - to stop hit a $\n");

   do

   {

      input_char = _getch();

      printf("Input character is %c, numerical value is %3d\n",

                 input_char, input_char);

   } while (input_char != '$');

   return 0;

}

/*  Result of execution

Hit any key - to stop hit a $

Input character is A, numerical value is  65

Input character is B, numerical value is  66

...

(The display depends on the input keys hit)

*/


Project 22:

 /* Chapter 9 - Program 2 - SINGLEIO.C */

                             /* Chapter 9 - Programming exercise 2 */

#include <stdio.h>

#include <conio.h>

 char storage[80];

 int main()

{

char c;

int index = 0;

    printf("Enter any characters, terminate program with X\n");

    do

   {

      c = _getch();           /* get a character                 */

      if (index < 79)         /* limit it to 79 characters       */

      {

         storage[index] = c;

         index++;

      }

      putchar(c);             /* display the hit key             */

   } while (c != 'X');

   storage[index] = 0;

   printf("%s\n", storage);

   printf("\nEnd of program.\n"); 

   return 0;

}

/* Result of execution

Enter any characters, terminate program with X

(The output depends on the characters you type in.)

End of program.

*/


Project 23:

#include <stdio.h>

 int main()

{

FILE *infile, *outfile, *printer;

char infilename[25], outfilename[25];

int  c;

    printf("Enter input file name ----> ");

   scanf("%s", infilename);

   infile = fopen(infilename, "r");

    printf("Enter output file name ---> ");

   scanf("%s", outfilename);

   outfile = fopen(outfilename, "w");

    printer = fopen("PRN", "w");

    do

   {

      c = getc(infile);

      if (c != EOF)

      {

         putchar(c);

         putc(c, outfile);

         putc(c, printer);

      }

   } while (c != EOF);

   fclose(printer);

fclose(infile);

   fclose(outfile);

   return 0;

}

 /* Result of execution

(This program writes to the printer, a file, and the monitor.)

*/


Project 24:

#include <stdio.h>

 

int main()

{

FILE *infile;

char c, infilename[25], inputline[100];

int line = 1;

    printf("Enter input file name ----> ");

   scanf("%s", infilename);

   infile = fopen(infilename, "r");

    printf("%5d", line);

   do

   {

      c = fgets(inputline, 100, infile);  /* read a line */

      if (c != NULL)

      {

         printf("%5d %s", line, inputline);

         line++;

      }

   } while (c != NULL);

    fclose(infile);

    return 0;

}

 /* Result of execution

(You will get a listing of the file on the monitor with line numbers)

*/


Project 25:

/* Chapter 10 - Program 7 */

#include <stdio.h>

 int main()

{

FILE *fp1;

char oneword[100], filename[25];

char *c;

    printf("Enter filename -> ");

   scanf("%s", filename);         /* read the desired filename */

   fp1 = fopen(filename, "r");

   if (fp1 == NULL)

   {

      printf("File doesn't exist\n");

      exit (1);

   }

   else

   {

      do

      {

         c = fgets(oneword, 100, fp1); /* get a line from the file */

         if (c != NULL)

            printf("%s", oneword);    /* display it on the monitor */

      } while (c != NULL);            /* repeat until NULL         */

   }

   fclose(fp1);

   return 0;

 /* Result of execution

(The output depends on weather you type in a good filename.)

*/


Project 26:

#include <stdio.h>

#include <string.h>

 struct

{

   char what[25];

   int legs, arms;

} object[6];

 int main()

{

int index;

    strcpy(object[0].what, "human being");

   object[0].legs = 2;

   object[0].arms = 2;

    strcpy(object[1].what, "dog");

   object[1].legs = 4;

   object[1].arms = 0;

    strcpy(object[2].what, "television set");

   object[2].legs = 4;

   object[2].arms = 0;

    strcpy(object[3].what, "chair");

   object[3].legs = 4;

   object[3].arms = 2;

    strcpy(object[4].what, "centipede");

   object[4].legs = 100;

   object[4].arms = 0;

    strcpy(object[5].what, "spider");

   object[5].legs = 6;

   object[5].arms = 0;

    for(index = 0 ; index < 6 ; index++)

   {

      printf("A %s has %d legs and %d arms.\n", object[index].what,

                  object[index].legs, object[index].arms);

}

   return 0;

}

 /* Result of execution

A human being has 2 legs and 2 arms.

A dog has 4 legs and 0 arms.

A television set has 4 legs and 0 arms.

A chair has 4 legs and 2 arms.

A centipede has 100 legs and 0 arms.

A spider has 6 legs and 0 arms.

*/


Project 27:

#include <stdio.h>

#include <string.h>

 struct

{

   char what[25];

   int legs, arms;

} object[6], *point;

 int main()

{

int index;

    strcpy(object[0].what, "human being");

   object[0].legs = 2;

   object[0].arms = 2;

    strcpy(object[1].what, "dog");

   object[1].legs = 4;

   object[1].arms = 0;

    strcpy(object[2].what, "television set");

   object[2].legs = 4;

   object[2].arms = 0;

strcpy(object[3].what, "chair");

   object[3].legs = 4;

   object[3].arms = 2;

    strcpy(object[4].what, "centipede");

   object[4].legs = 100;

   object[4].arms = 0;

    strcpy(object[5].what, "spider");

   object[5].legs = 6;

   object[5].arms = 0;

    point = object;

   for(index = 0 ; index < 6 ; index++)

   {

      printf("A %s has %d legs and %d arms.\n", point->what,

                      point->legs, point->arms);

      point++;

   }

   return 0;

}

/* Result of execution

A human being has 2 legs and 2 arms.

A dog has 4 legs and 0 arms.

A television set has 4 legs and 0 arms.

A chair has 4 legs and 2 arms.

A centipede has 100 legs and 0 arms.

A spider has 6 legs and 0 arms.

*/


Project 28:

#include <stdio.h>

#include <malloc.h>

 struct child

{

char initial;    /* last name initial      */

   int age;         /* childs age             */

   int grade;       /* childs grade in school */

} *boy, *girl;

int main()

{

   boy = (struct child *)malloc(sizeof(struct child));

    boy->initial = 'R';

   boy->age = 15;

   boy->grade = 75;

    girl = (struct child *)malloc(sizeof(struct child));

    girl->age = boy->age - 1;  /* she is one year younger */

   girl->grade = 82;

   girl->initial = 'H';

    printf("%c is %d years old and got a grade of %d\n",

           girl->initial, girl->age, girl->grade);

    printf("%c is %d years old and got a grade of %d\n",

           boy->initial, boy->age, boy->grade);

    return 0;

}

/* Result of execution

H is 14 years old and got a grade of 82

R is 15 years old and got a grade of 75

*/


Project 29:

#include <stdio.h>

#include <malloc.h>

 struct child

{

   char initial;

int age;

   int grade;

} *kids[12];

 

int main()

{

int index;

for (index = 0 ; index < 12 ; index++)

   {

      kids[index] = (struct child *)malloc(sizeof(struct child));

      kids[index]->initial = 'A' + index;

      kids[index]->age = 16;

      kids[index]->grade = 84;

   }

kids[3]->age = kids[5]->age = 17;

   kids[2]->grade = kids[6]->grade = 92;

   kids[4]->grade = 57;

    *kids[10] = *kids[4];               /* Structure assignment  */

    for (index = 0 ; index < 12 ; index++)

      printf("%c is %d years old and got a grade of %d\n",

             kids[index]->initial, kids[index]->age,

             kids[index]->grade); 

 return 0;

}

/* Result of execution

A is 16 years old and got a grade of 84

B is 16 years old and got a grade of 84

C is 16 years old and got a grade of 92

D is 17 years old and got a grade of 84

E is 16 years old and got a grade of 57

F is 17 years old and got a grade of 84

G is 16 years old and got a grade of 92

H is 16 years old and got a grade of 84

I is 16 years old and got a grade of 84

J is 16 years old and got a grade of 84

E is 16 years old and got a grade of 57

L is 16 years old and got a grade of 84

*/

Home