C Primer Plus
Fifth edition
12th chapter of the program list
#include <stdio.h>
int main ()
{
int x=30;
printf ("X in Outer block:%d\n", x);
{
int x=77;
printf ("X in inner block:%d\n", x);
}
printf ("X in Outer block:%d\n", x);
while (x++<33)
{
int x=100;
x + +;
printf ("X in While loop:%d\n", X);
}
printf ("X in Outer loop:%d\n", x);
return 0;
}
2.
#include <stdio.h>
int main ()
{
int n = 10;
printf ("Initially, n =%d \ n");
for (int n = 1; n < 3; n++)
printf ("loop 1:n =%d \ n", n.);
printf ("After loop 1, n =%d \ n", n.);
for (int n = 1; n < 3; n++)
{
printf ("Loop 2 index n =%d \ n");
int n = 30;
printf ("loop 2:n =%d \ n", n.);
n++;
}
printf ("After loop 2, n =%d \ n", n.);
return 0;
}
Program Listing 12.3
#include <stdio.h>
void Trysat (void);
int main (void)
{
int count;
for (count=1;count<=3;count++)
{
printf ("Here Comes lterration%d:
\ n ", count);
Trysat ();
}
return 0;
}
void Trysat (void)
{
int fade=1;
static int stay=1;
printf ("fade=%d and stay=%d\n", fade++,stay++);
}
Program Listing 12.4
#include <stdio.h>
int units=0;
void critic (void);
int main (void)
{
extern int units;
printf ("How many pounds to a firkin of butter?")
\ n ");
scanf ("%d", &units);
while (units!=56)
Critic ();
printf ("You must has looked it up! %d
\ n ", units);
return 0;
}
void critic (void)
{
printf ("Noluck");
scanf ("%d", &units);
}
#include <stdio.h>
void Report_count ();
void accumulate (int k);
int count=0;
int main (void)
{
int value;
register int i;
printf ("Enter a positive integer:");
while (scanf ("%d", &value) ==1&&value>0)
{
++count;
for (i=value;i>=0;i--)
accumulate (i);
printf ("Enter a positive integer");
}
Report_count ();
return 0;
}
void Report_count ()
{
printf ("Loop executed%d times\n", count);
}
#include <stdio.h>
extern int count;
static int total=0;
void accumulate (int k);
void accumulate (int k)
{
static int subtotal=0;
if (k<=0)
{
printf ("Loop cycle:%d\n", count);
printf ("subtotal:%d; total:%d
\ n ", subtotal,total);
subtotal=0;
}
Else
{
Subtotal+=k;
Total+=k;
}
}
Program Listing 12.7
static unsigned long int next=1;
int rand0 (void)
{
next=next*1103515245+12345;
return (unsigned int) (next/65536)%32768;
}
Program Listing 12.8
#include <stdio.h>
extern int rand0 (void);
int main (void)
{
int count;
for (count=0;count<5;count++)
printf ("%hd\n", Rand0 ());
return 0;
}
Program Listing 12.9
#include <stdio.h>
extern void Srand1 (unsigned int x);
extern int rand1 (void);
int main (void)
{
int count;
unsigned seed;
printf ("Please enter your choice for seed.\n");
while (scanf ("%u", &seed) ==1)
{
Srand1 (seed);
for (count=0;count<5;count++)
printf ("%hd\n", Rand1 ());
printf ("Please enter next seed\n");
}
printf ("done\n");
return 0;
}
Program Listing 12.10
static unsigned long int next=1;
int rand1 (void)
{
next=next*1103515245+12345;
return (unsigned int) (next/65536)%32768;
}
void srand1 (unsigned int seed)
{
Next=seed;
}
Program Listing 9.11
#include "diceroll.h"
#include <stdio.h>
#include <stdlib.h>
int roll_count=0;
static int Rollem (int sides)
{
int roll;
Roll=rand ()%sides+1;
++roll_count;
return roll;
}
int roll_n_dice (int dice,int sides)
{
int D;
int total=0;
if (sides<2)
{
printf ("Need at least 2 sides.\n");
Return-2;
}
if (dice<1)
{
printf ("Need at least 1 die.\n");
return-1;
}
for (d=0;d<dice;d++)
Total+=rollem (sides);
return total;
}
Program Listing 9.12
extern int roll_count;
int roll_n_dice (int dice,int sides);
Program Listing 9.13
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h"
int main (void)
{
int dice,roll;
int sides;
Srand ((unsigned int) time (0));
printf ("Enter the number of sides per die.0 to
Stop.\n ");
while (scanf ("%d", &sides) ==1&&sides>0)
{
printf ("How many dice?\n");
scanf ("%d", &dice);
Roll=roll_n_dice (Dice,sides);
printf ("You have rolled a%d using%d%d
Sided dice.\n ", roll,dice,sides);
printf ("How many sides?\n");
}
printf ("The Rollem%d times.\n", roll_count);
printf ("good");
return 0;
}
Program Listing 12.14
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
Double *ptd;
int Max;
int number;
int i=0;
Puts ("What is the?");
scanf ("%d", &max);
Ptd= (double*) malloc (max*sizeof (double));
if (ptd==null)
{
Puts ("Memory allocation faild\n");
Exit (Exit_failure);
}
Puts ("Enter the Values");
while (I<MAX&&SCANF ("%lf", &ptd[i]) ==1)
++i;
printf ("Here is your%d entries\n", number=i);
for (i=0;i<number;i++)
{
printf ("%7.2f", Ptd[i]);
if (i%7==6)
Putchar (' \ n ');
}
if (i%7!=0)
Putchar (' \ n ');
Puts ("done.");
Free (PTD);
return 0;
}
C Primer Plus 12th Chapter Program List ... 2015.5.10