
cd to c:\gcc-2.95.2 and run
mingw32.bat to add the path. If you want to run gcc from
any folder, add the folder
c:\gcc-2.95.2
to the PATH environmental variable (details in class).
cd c:\gcc-2.95.2 mingw32 |
gcc MyProgram.c |
g++ MyProgram.cpp |
g77 MyProgram.f |
#include <stdio.h>
int main()
{
int i ;
for (i=1; i>0; i++) printf("loop");
return 0;
}
|
for (initial value; condition ; counter increment) statement; |
#include <stdio.h>
int main(){
int i;
for (i=0; i< 10; i++) printf("i=%d\n",i);
return 0;
}
|
|
#include <stdio.h>
int main()
{
int i, s = 0;
for (i=0; i<= 100; i++) s = s + i;
printf("Sum = %d\n", s);
return 0;
}
|
sum = 0.0; for (i=0; i<= 100; i++) sum = sum + function(i); |
|
#include <stdio.h>
int main()
{
int i=0;
while (i<10) i++;
printf("%d\n",i);
return 0;
}
|
#include <stdio.h>
int main()
{
int i=0;
while (i<10)
{i++;
printf("i = %d\n",i);
}
printf("%d\n",i);
return 0;
}
|
#include <stdio.h>
int main()
{
int input_value;
do
{ printf("Enter 1 for yes, 0 for no :");
scanf("%d", &input_value);
} while (input_value != 1 && input_value != 0);
return 0;
}
|
#include <stdio.h>
int main()
{
int i;
printf("Enter an integer="); scanf("%d", &i);
switch(i){
case 1: printf("a is 1\n");break;
case 2: printf("a is 2\n");break;
default: printf("a is neither 1 nor 2\n");break;
}
return 0;
}
|
| Wrong | Correct |
| if (a=0) printf(" Not valid !\n"); | if (a==0) printf(" illegal !\n"); |
| int a=10,b=20; float c; c=a/b; | float a=10, b=20, c; c=a/b; |
| gcc math1.c | gcc -lm math1.c |
| scanf("%f", a) | scanf("%f", &a) |
| for (i=0, i<10, i++) | for (i=0; i<10; i++) |
| for (i==0, i<10, i++) | for (i=0; i<10; i++) |
|
#include <stdio.h>
#include <math.h>
int main()
{
int i,n;
float sum=0.0;
printf("Enter # of iterations = ");
scanf("%d", &n);
for (i=1;i<n;i++)
sum = sum + pow(-1, i+1)/i;
printf("Approximation of ln(2)= %f. ", sum);
printf("Exact value of ln(2)= %f.\n", log(2));
return 0;
}
|
#include <stdio.h>
void write()
{
printf("Hello world !\n");
}
int main()
{
write();
return 0;
}
|
#include <stdio.h>
float cube(float x)
{
return x*x*x;
}
int main()
{
float x;
printf("Enter x = "); scanf("%f",&x);
printf("The cube of x is %f.\n", cube(x));
return 0;
}
|
#include <stdio.h>
#include <math.h>
float power(float x, float y)
{
return exp(y*log(x));
}
int main()
{
float x,y;
printf("Enter x and y separated by space =");
scanf("%f %f",&x,&y);
if (x<0)
{
printf("x must be positive !!\n");
return 0;
}
printf("%f to power of exponent %f is %f.\n", x, y, power(x,y));
return 0;
}
|
#include <stdio.h>
float f(int n)
{
int i; float sum=0;
for (i=1;i < n;i++) sum=sum+1.0/i;
return sum;
}
int main()
{
float sum=0;
int i;
for (i=1;i < 20;i++) sum=sum+1.0/i/i;
printf("%f %f\n", sum, f(20));
return 0;
}
|
#include <stdio.h>
int i=20;
void f()
{
printf("%d\n",i);
}
int main()
{
int i=30;
printf("%d\n", i);
f();
return 0;
}
|
|
Suppose a newly-born pair of rabbits, one male, one female, are put in a field.
Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was...
How many pairs will there be in two years (a24 = ?) ?
Code:
#include <stdio.h>
int fibo(int n)
{
if (n==1) return 1;
if (n==2) return 1;
return fibo(n-1) + fibo(n-2);
}
int main()
{
int i;
printf("Enter n = "); scanf("%d", &i);
printf("%d\n", fibo(i));
return 0;
}
|
|
|
#include <stdio.h>
int mysum(int n)
{
if (n==0) return 0;
return n + mysum(n-1);
}
int main()
{
int n;
printf("Enter n = ");
scanf("%d", &n);
printf("1+2+....+%d =%d \n", n, mysum(n));
return 0;
} |
| (1) |
#include <stdio.h>
int mysequence(int n)
{
if (n==1) return 1;
if (n==2) return 3;
return 4*mysequence(n-1)-6*mysequence(n-2);
}
int main()
{
int n;
printf("Enter n = ");
scanf("%d", &n);
printf("%d\n",mysequence(n));
return 0;
}
|