int main()
{
int i;
int squares[11]; /* [0..10]; [0] ignored */
int cubes[11];
/* fill arrays: */
for(i = 1; i <= 10; i = i + 1)
{
squares[i] = i * i;
cubes[i] = i * i * i;
}
/* print table: */
printf("n\tsquare\tcube\n");
for(i = 1; i <= 10; i = i + 1)
printf("%d\t%d\t%d\n", i, squares[i], cubes[i]);
return 0;
}
the array-of-squares program to also print cubes.
the dice-rolling program without arrays
int roll2, roll3, roll4, roll5, roll6, roll7, roll8, roll9,
roll10, roll11, roll12;
sum = d1 + d2; /* sum two die rolls */
if(sum == 2)
roll2 = roll2 + 1;
else (sum == 3)
roll3 = roll3 + 1;
else (sum == 4)
roll4 = roll4 + 1;
else (sum == 5)
roll5 = roll5 + 1;
...
Related Links :
some improvements to the prime number printing program.
int main()
{
int i, j;
double sqrti;
printf("%d\n", 2);
for(i = 3; i <= 100; i = i + 2)
{
sqrti = sqrt(i);
for(j = 2; j < i; j = j + 1)
{
if(i % j == 0)
break; /* not prime */
if(j > sqrti)
{ /* prime */
printf("%d\n", i);
break;
}
}
}
return 0;
}
Related Links :
Program to show a randrange function.
Here is a straightforward implementation of randrange2:
#include
int randrange2(int m, int n)
{
return rand() % (n - m + 1) + m;
}
Here is one using the suggested ``better way of reducing the range of the rand function'': #include
int randrange2(int m, int n)
{
return rand() / (RAND_MAX / (n - m + 1) + 1) + m;
}
Notice that I've replaced N in the suggested general form with the expression n - m + 1. You could implement the simpler randrange function either as
int randrange(int n)
{
return rand() % n + 1;
}
or, using the improvement, int randrange(int n)
{
return rand() / (RAND_MAX / n + 1) + 1;
}
or, by writing it ``on top of'' the more general randrange2 you already wrote, int randrange(int n)
{
return randrange2(1, n);
}
The various ``fudge factors'' in these expressions deserve some explanation. The first one is straightforward: The + 1 in (n - m + 1) simply gives us the number of numbers in the range mn, including m and n. (Leaving out the + 1 in this case is the classic example of a fencepost error, named after the old puzzle, ``How many pickets are there in a picket fence ten feet long, with the pickets one foot apart?'') to
The other +1 is a bit trickier. First let's consider the second implementation of randrange. We want to divide rand's output by some number so that the results will come out in the range 0 to n - 1. (Then we'll add in 1 to get numbers in the range 1 through n.) Left to its own devices, randRAND_MAX (where RAND_MAX is a constant defined for us in
RAND_MAX / n
that is, if we were to write rand() / (RAND_MAX / n)
then when rand returned RAND_MAX, the division could yield exactly n, which is one too many. (This wouldn't happen too often--only when rand returned that one value, its maximum value--but it would be a bug, and a hard one to find, because it wouldn't show up very often.) So if we add one to the denominator, that is, divide by the quantity RAND_MAX / n + 1
then when rand returns RAND_MAX, the division will yield a number just shy of n, which will then be truncated to n - 1, which is just what we want. We add in 1, and we're done. In the case of the more general randrange2, everything we've said applies, with n replaced by n - m + 1. Dividing by
RAND_MAX / (n - m + 1)
would occasionally give us a number one too big (n + 1, after adding in m), so we divide by RAND_MAX / (n - m + 1) + 1
instead. Finally, just two lines in the dice-rolling program would need to be changed to make use of the new function:
d1 = randrange(6);
d2 = randrange(6);
or d1 = randrange2(1, 6);
d2 = randrange2(1, 6);
The answer to the extra-credit portion of the exercise is that under some compilers, the output of the rand function is not quite as random as you might wish. In particular, it's not uncommon for the rand funciton to produce alternately even and odd numbers, such that if you repeatedly compute rand % 2, you'll get the decidedly non-random sequence 0, 1, 0, 1, 0, 1, 0, 1... . It's for this reason that the slightly more elaborate range-reduction techniques involving the constant RAND_MAX are recommended.
Related Links :
a function celsius() to convert degrees Fahrenheit to degrees Celsius. Use it to print a Fahrenheit-to-Centigrade table for -40 to 220 degrees Fahrenh
Here is the function:
double celsius(double f)Here is the driver program:
{
return 5. / 9. * (f - 32);
}
#include
double celsius(double);
int main()
{
double f;
for(f = -40; f <= 220; f = f + 10)
printf("%f\t%f\n", f, celsius(f));
return 0;
}
Related Links :
a function to compute the factorial of a number, and use it to print the factorials of the numbers 1-7.
Here is the function:
int factorial(int x)
{
int i;
int fact = 1;
for(i = 2; i <= x; i = i + 1)
fact = fact * i;
return fact;
}
Here is a driver program: #include
int factorial(int);
int main()
{
int i;
for(i = 1; i <= 7; i = i + 1)
printf("%d %d\n", i, factorial(i));
return 0;
}
The answer to the ``extra credit'' problem is that to portably compute factorials beyond factorial(7), it would be necessary to declare the factorial() function as returning long int (and to declare its local fact variable as long int as well, and to use %ld in the call to printf). 8! (``eight factorial'') is 40320, but remember, type int is only guaranteed to hold integers up to 32767.
(Some machines, but not all, have ints that can hold more than 32767, so computing larger factorials on those machines would happen to work, but not portably. Some textbooks would tell you to ``use long int if your machine has 16-bit ints,'' but why write code two different ways depending on what kind of machine you happen to be using today? I prefer to say, ``Use long int if you would like results greater than 32767.'')
Related Links :
printnchars function, and use it to rewrite the triangle-printing program.
void printnchars(int ch, int n)Here is the rewritten triangle-printing program:
{
int i;
for(i = 0; i < n; i++)
printf("%c", ch);
}#include
int main()
{
int i;
for(i = 1; i <= 10; i = i + 1)
{
printnchars('*', i);
printf("\n");
}
return 0;
}
Related Links :
Program to find square() function and use it to print the squares of the numbers 1-10.
{
return x * x;
}
Here is a loop and main program to call it:
int square(int);
int main()
{
int i;
for(i = 1; i <= 10; i = i + 1) printf("%d %d\n", i, square(i)); return 0; }
Related Links :
Program to sum the elements of an array of int.
int sumnum(int a[], int n)
{
int i;
int sum = 0;
for(i = 0; i < i =" i">
sum = sum + a[i];
return sum;
}
Here is a test program to call it: #include
int a[] = {1, 2, 3, 4, 5, 6};
int sumnum(int [], int);
int main()
{
printf("%d\n", sumnum(a, 6));
return 0;
}
Related Links :
write the simple graphics program to print ``open'' boxes.
int printbox(int n)
{
int i, j;
for(j = 0; j < n; j = j + 1)
printf("*");
printf("\n");
for(i = 0; i < n-2; i = i + 1)
{
printf("*");
for(j = 0; j < n-2; j = j + 1)
printf(" ");
printf("*\n");
}
for(j = 0; j < n; j = j + 1)
printf("*");
printf("\n");
return 0;
}
Related Links :
Modify the array-of-squares program to also print cubes.
int main()
{
int i;
int squares[11]; /* [0..10]; [0] ignored */
int cubes[11];
/* fill arrays: */
for(i = 1; i <= 10; i = i + 1)
{
squares[i] = i * i;
cubes[i] = i * i * i;
}
/* print table: */
printf("n\tsquare\tcube\n");
for(i = 1; i <= 10; i = i + 1)
printf("%d\t%d\t%d\n", i, squares[i], cubes[i]);
return 0;
}
Related Links :
What is the difference between a defining instance and an external declaration?
A defining instance is a declaration of a variable or function that actually defines and allocates space for that variable or function. In the case of a variable, the defining instance may also supply an initial value, using an initializer in the declaration. In the case of a function, the defining instance supplies the body of the function.
An external declaration is a declaration which mentions the name and type of a variable or function which is defined elsewhere. An external declaration does not allocate space; it cannot supply the initial value of a variable; it does not need to supply the size of an array; it does not supply the body of a function. (In the case of functions, however, an external declaration may include argument type information; in this case it is an external prototype declaration.)
Related Links :
a program to print the first 10 Fibonacci numbers.
#include
int main()
{
int i;
int fibonacci = 1;
int prevfib = 0;
int tmp;
for(i = 1; i <= 10; i = i + 1)
{
printf("%d %d\n", i, fibonacci);
tmp = fibonacci;
fibonacci = fibonacci + prevfib;
prevfib = tmp;
}
return 0;
}
Related Links :
a program to print the first 7 positive integers and their factorials.
#include "stdio.h"
int main()
{
int i;
int factorial = 1;
for(i = 1; i <= 7; i = i + 1)
{
factorial = factorial * i;
printf("%d %d\n", i, factorial);
}
return 0;
}
Related Links :
Print out the 8 points of the compass, based on the x and y components of the direction of travel.
if(x > 0)
{
if(y > 0)
printf("Northeast.\n");
else if(y < 0)
printf("Southeast.\n");
else printf("East.\n");
}
else if(x < 0)
{
if(y > 0)
printf("Northwest.\n");
else if(y < 0)
printf("Southwest.\n");
else printf("West.\n");
}
else {
if(y > 0)
printf("North.\n");
else if(y < 0)
printf("South.\n");
else printf("Nowhere.\n");
}
Related Links :
a program to compute the average of the ten numbers 1, 4, 9, ..., 81, 100.
int main()
{
int i;
float sum = 0;
int n = 0;
for(i = 1; i <= 10; i = i + 1)
{
sum = sum + i * i;
n = n + 1;
}
printf("the average is %f\n", sum / n);
return 0;
}
Related Links :
What are the definitions of the ``Boolean'' values true and false in C?
Related Links :
A program to print the numbers from 1 to 10 and their squares.
#include
int main()
{
int i;
for(i = 1; i <= 10; i = i + 1)
printf("%d %d\n", i, i * i);
return 0;
}
Exercise 4. Write a program to print a simple triangle of asterisks.
#include "stdio.h"
int main()
{
int i, j;
for(i = 1; i <= 10; i = i + 1)
{
for(j = 1; j <= i; j = j + 1)
printf("*");
printf("\n");
}
return 0;
}
Related Links :
What is the function of the semicolon in a C statement?
Related Links :
What is the difference between the constants 7, '7', and "7"?
Related Links :
What is the difference between the constants 7, '7', and "7"?
Related Links :
Why is the line #include "stdio.h" at the top of a C source file for?
Related Links :
a short program to read two lines of text, and concatenate them using strcat.
#include "stdio.h"
#include "string.h"
/* for strcpy and strcat */
#define MAXLINE 100
extern int getline(char [], int);
int main()
{
char string1[MAXLINE], string2[MAXLINE];
int len1, len2;
char newstring[MAXLINE*2];
printf("enter first string:\n");
len1 = getline(string1, 100);
printf("enter second string:\n");
len2 = getline(string2, 100);
if(len1 == EOF || len2 == EOF)
exit(1);
strcpy(newstring, string1);
strcat(newstring, string2);
printf("%s\n", newstring);
return 0;
}
Exercise 5. Write a function to find a substring in a larger string and replace it with a different substring.
Here is one way. (Since the function doesn't return anything, I've defined it with a return type of void.)
void replace(char string[], char from[], char to[])This code is very similar to the mystrstr function in the notes, chapter 10, section 10.4, p. 8. (Since strstr's job is to find one string within another, it's a natural for the first half of replace.)
{
int start, i1, i2;
for(start = 0; string[start] != '\0'; start++)
{
i1 = 0;
i2 = start;
while(from[i1] != '\0')
{
if(from[i1] != string[i2])
break;
i1++;
i2++;
}
if(from[i1] == '\0')
{
for(i1 = 0; to[i1] != '\0'; i1++)
string[start++] = to[i1];
return;
}
}
}
Extra credit: Think about what replace() should do if the from string appears multiple times in the input string.
Our first implementation replaced only the first occurrence (if any) of the from string. It happens, though, that it's trivial to rewrite our first version to make it replace all occurrences--just omit the return after the first string has been replaced:
void replace(char string[], char from[], char to[])
{
int start, i1, i2;
for(start = 0; string[start] != '\0'; start++)
{
i1 = 0;
i2 = start;
while(from[i1] != '\0')
{
if(from[i1] != string[i2])
break;
i1++;
i2++;
}
if(from[i1] == '\0')
{
for(i1 = 0; to[i1] != '\0'; i1++)
string[start++] = to[i1];
}
}
}
Related Links :
If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE
More Useful Topics... |
|
History Of C..
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.
My 1st Program...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}
Next Step...
#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");
printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");
}