Você está na página 1de 21

Practice!!!

1-/* file: args_1.c The following program demonstrates processing a 2 dimensional


array*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char * argv[])
{
int i;
int res_i = 4;
double res_d = 4.5;

printf("\nProgram to demonstrate command line arguments\n");


printf(" [usage: $ prog_name string1 string2 integer3 double4]\n");
/* display what is passed in the arguments */
/* the array argv is NEVER empty */
/* at the very least it has the name of the program you are running */
printf("prog = \"%s\"\t< this is the program name itself, at argv[0]\n\n", argv[0]);
/* argc will be the number of strings in the array argv[] */
printf("Notice all arguments are string, none are numeric\n");
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++){
printf("argv[%d] = \"%s\"\n", i, argv[i]);
}
printf("\n");
/* all aparameters are passed as string. */
/* even if you type in a number it will be stored as a string in argv[] */
/* any numeric values must be converted to an actual integer or double */
/* the conversion is done using either atoi() or atod() */
if (argc > 4)
{
/* let's assume the 3rd parameter is an integer, and the 4th is a double */
printf("argv[3] as a string = [%s]\n", argv[3]);
printf("argv[4] as a string = [%s]\n", argv[4]);

/* after conversion to an integer, we can perform arithmetic on the number */


res_i = atoi(argv[3]);
printf("arg[3] as an integer, stored in res_i = %d\n", res_i);

res_d = atof(argv[4]);
printf("arg[3] as a double, stored in res_d = %lf\n", res_d);
}
else

1
{
printf("Sorry, but to demonstate the numeric portion, you must pass\n");
printf("an integer and a double as 3rd and 4th parameters\n");
}
return (0);
}

2-/*file: arrays2.c The following program demonstrates dynamic arrays.*/


#include <stdio.h>
#include <stdlib.h> /*important to include stdlib.h in order to use calloc and malloc*/

int main(void)
{

int size, i;
/* 1. declare a pointer to the array */
double *array;
int *array2;

printf ("Enter the size of the array: ");


scanf ("%d", &size);

/* 2. allocate the array in the heap */


array = (double *) calloc(size, sizeof(double));

if (array == NULL)
{
printf("There was an error allocating this much memory for array. Try
less next time.\n");
}
else
{
printf("calloc() initializes values before returning a refence to it:");
for (i = 0; i < size; i++){
printf("\n%lf", array[i]);
}
printf("\n");

/* 3. use the array normally */


for (i = 0; i < size; i++){
printf("Enter a double: ");
scanf("%lf", &array[i]);
}

printf("\nYou entered the following %d doubles", size);


for (i = 0; i < size; i++){

2
printf("\n%lf", array[i]);
}
printf("\n\nNow we need to free the memory we allocated with
free(array)\n");
/* 4. free the heap memory */
free (array);
}

printf ("\nEnter the size of the array2: ");


scanf ("%d", &size);

/* 2. allocate the array in the heap */


array2 = (int *) malloc (size * sizeof(int) );

if (array2 == NULL)
{
printf("There was an error allocating this much memory for array2.
Try less next time.\n");
}
else
{
printf("malloc() DOES NOT initializes values in array2 before
returning a refence to it:");
for (i = 0; i < size; i++){
printf("\n%d", array2[i]);
}
printf("\n");

/* 3. use the array normally */


for (i = 0; i < size; i++){
printf("Enter an int: ");
scanf("%d", &array2[i]);
}
printf("\nYou entered the following %d integers", size);
for (i = 0; i < size; i++){
printf("\n%d", array2[i]);
}
printf("\n\nNow we need to free the memory we allocated with
free(array2)\n");
/* 4. free the heap memory */
free (array2);
}}

3
3-/* file: strings1.c The following program demonstrates character arrays (strings)
and is an intro 2 dimensional arrays.*/
#include <stdio.h>
int main(void)
{
/* single character */
char c1 = 'D';

/* an array of 20 characters, creating a string */


char c2[20];

/* declaring a char array, and initializing its contents */


/* notice we do not need specify the size since it's implied by the initialization
values */
/* - contents initialized 1 character at a time */
/* - notice the trailing '\0', indicting ed of string */
char c3[] = {'h', 'e', 'l', 'l', 'o', '\0'};
/* - contents initialized as a complete string, which includes the trailing '\0' */
char c4[] = "goodbye";

/* multidimensional arrays */
char c5[3][10] = {"yes", "no", "maybe"};
/* - notice we did not need to spcify the sizeof the 1st index, but we do need to
specify the 2nd index */
char c6[][30] = {"toronto", "montreal", "ottawa", "vancouver"};

int i = 0;
/* dispay a single character */
printf("\nc1 = %c \t%d", c1, sizeof(c2));

/* initialize c2 with 'x' values */


for (i = 0; i < sizeof(c2); i++){
c2[i] = 'x';
}
/* place a trailing '\0' to end the string */
c2[sizeof(c2)] = '\0';

/* notie we are using the %s format to display a string */


printf("\nc2 is all x's = [%s]", c2);

/* lets set each element of c2 individually */


/* - notice that no ending '\0' is specified, and the string ends at 30's character */
c2[0] = 'C';
c2[1] = 'P';
c2[2] = 'S';
c2[3] = '1';

4
c2[4] = '2';
c2[5] = '5';
printf("\nc2 = [%s]", c2);

/* let's set the 6th character of c2 to '\0', and specify a character after the ending '\0' */
/* - notice that the string now ends after "CPS125", and the 'F' is not displayed becuase
*/ /* it falls after the '\0' character */
c2[6] = '\0';
c2[7] = 'F';
printf("\nc2 = [%s]", c2);
printf("\nc3 = %s", c3);
printf("\nc4 = %s < notice that the '\\0' was entered properly when c4 was initialized",
c4);
/* display each individual character of c4 */
printf("\n-------------------\n");
for (i = 0; i < sizeof(c4)-1; i++){
printf("\nc4[%d] = %c", i, c4[i]);
}

/* display each string in the multidemensional array, which holds 3 string */


printf("\n");
printf("\nc5[0] = %s", c5[0]);
printf("\nc5[1] = %s", c5[1]);
printf("\nc5[2] = %s", c5[2]);

/* remember that elements in an array are stored sequentially in memory. */


/* if we display the 30 memory locations starting at address that c5 points */
/* to, we can see the entire array, 3 string of 10 characters. */
/* IMPORTANT: you would usually not do this. It is here only to illustrate */
/* memory allocations for arrays. */
printf("\n");
for (i = 0; i < 30; i++){
printf("\nc5[0][%d] = %c", i, c5[0][i]);
}

/* print out elements in c6 */


printf("\n");
for (i = 0; i < 4; i++){
printf("\nc6[%d] = %s", i, c6[i]);
}

printf("\n");
}

5
4-/* file: array_2d_5.c The following program demonstrates multiplication of a
square matrix and a scalar array, and 2 square matricies*/
#include <stdio.h>
#define DIM 3
/* matrix by a scalar */
void m1 ()
{
int M[DIM][DIM] = {{3,1,9}, {8,4,5}, {6,2,7}};
int N[DIM][1] = {4,3,5};
int prod[DIM];
int i, k, j;
j = 0;
printf ("\n - - - Matrix * Scalar - - -\n");
/* perform the multiplication */
for (i=0; i<DIM; ++i)
{
prod[i] = 0;
for (k=0; k<DIM; ++k)
{
prod[i] = prod[i] + M[i][k] * N[j][k];
}
}
/* display scalar */
printf ("The Scalar is:\n");
for (k=0; k<DIM; ++k)
{
printf ("%d\n", N[j][k]);
}
printf ("\n");
/* display matrix 2 */
printf ("The matrix 2 is:\n");
for (i=0; i<DIM; ++i)
{
for (k=0; k<DIM; ++k)
printf ("%d\t", M[i][k]);
printf ("\n");
}
printf ("\n");
/* display product matrix */
printf ("The product is:\n");
for (i=0; i<DIM; ++i)
{
printf ("%d\t", prod[i]);
}
printf ("\n");
}

6
/* matrix by a matrix */
void m2 ()
{
int M[DIM][DIM] = {{3,1,9}, {8,4,5}, {6,2,7}};
int N[DIM][DIM] = {{4,9,2}, {3,7,1}, {5,6,8}};
int prod2[DIM][DIM];
int i, k, j;
j = 0;
printf ("\n - - - Matrix * Matrix - - -\n");
/* perform the multiplication */
for (i=0; i<DIM; ++i)
{
for (k=0; k<DIM; ++k)
{
prod2[i][k] = 0;
for (j=0; j<DIM; ++j)
{
prod2[i][k] = prod2[i][k] + M[i][j] * N[j][k];
}
}
}
/* display matrix 1 */
printf ("The matrix 1 is:\n");
for (i=0; i<DIM; ++i)
{
for (k=0; k<DIM; ++k)
printf ("%d\t", M[i][k]);
printf ("\n");
}
printf ("\n");
/* display matrix 2 */
printf ("The matrix 2 is:\n");
for (i=0; i<DIM; ++i)
{
for (k=0; k<DIM; ++k)
printf ("%d\t", N[i][k]);
printf ("\n");
}
printf ("\n");
/* display matrix */
printf ("The product is:\n");
for (i=0; i<DIM; ++i)
{
for (k=0; k<DIM; ++k)
printf ("%d\t", prod2[i][k]);
printf ("\n");

7
}
printf ("\n");
}
int main (void){
m1();
m2();
return (0);
}

5-/* file: test3_rev1.c The following program demonstrates pointers*/


#include <stdio.h>
#include <math.h>
void check(int *p);
int main(void){
int x = 5;
int *a;
printf("\n[%d] [%d]\n", x, a);
printf("\n[%d] [%d]\n", x, *a);

a = &x;
printf("\n[%d] [%d]\n", x, a);
printf("\n[%d] [%d]\n", x, *a);

check(a);
printf("\n[%d] [%d]\n", x, a);
printf("\n[%d] [%d]\n", x, *a);

x = 5;
printf("\n[%d] [%d]\n", x, a);
printf("\n[%d] [%d]\n", x, *a);

check(&x);
printf("\n[%d] [%d]\n", x, a);
printf("\n[%d] [%d]\n", x, *a);

printf("round = [%lf] [%lf]\n", round(2.3), round(3.8));


return 0;
}
void check(int *p)
{
*p = 45;
}

8
6-/* file: array_2d_1.c The following program demonstrates processing a 2
dimensional array */
#include <stdio.h>
#define MAXSIZE 100
int search2d (int array[][MAXSIZE], int value, int size, int *foundcol)
{
/* foundrow will return the row where the value was found
or -1 if value not found */
/* *foundcol will be returning the column where the value
was found. It remains undefined if not found */

int i, j, foundrow;
/* initialize found at -1, if value not found, stays -1 */
foundrow = -1;
/* search until found or until end of array */
for (i=0; i<size; ++i)
for (j=0; j<size; ++j)
{
if (array[i][j] == value)
{
foundrow = i; /* I have found it! */
*foundcol = j;
}
}
return (foundrow);
}
int main (void){
int x[100][100], value, row, col, size, i, j;
value = 99; /* the value I am searching */
/* reading size from file */
printf("Enter size: ");
scanf ("%d", &size);
/* fills array from file using redirection */
printf("Enter values: \n");
for (i=0; i<size; ++i)
for (j=0; j<size; ++j)
scanf ("%d", &x[i][j]);
/* calling the search function */
row = search2d (x, value, size, &col);

if (row >= 0)
printf ("%d found at row %d / column %d.\n", value, row, col);
else
printf ("%d not found in the array.\n", value);
return (0);
}

9
7-/* file:str_03.c The card shuffling and dealing program using structures */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* card structure definition */
struct card {
const char *face; /* define pointer face */
const char *suit; /* define pointer suit */
}; /* end structure card */

typedef struct card Card; /* new type name for struct card */

/* prototypes */
void fillDeck( Card * const wDeck, const char * wFace[],
const char * wSuit[] );
void shuffle( Card * const wDeck );
void deal( const Card * const wDeck );

int main()
{
Card deck[ 52 ]; /* define array of Cards */

/* initialize array of pointers */


const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King"};

/* initialize array of pointers */


const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};

srand( time( NULL ) ); /* randomize */

fillDeck( deck, face, suit ); /* load the deck with Cards */


shuffle( deck ); /* put Cards in random order */
deal( deck ); /* deal all 52 Cards */

return 0; /* indicates successful termination */

} /* end main */
/* place strings into Card structures */
void fillDeck( Card * const wDeck, const char * wFace[],
const char * wSuit[] )
{
int i; /* counter */

/* loop through wDeck */

10
for ( i = 0; i <= 51; i++ ) {
wDeck[ i ].face = wFace[ i % 13 ];
wDeck[ i ].suit = wSuit[ i / 13 ];
} /* end for */

} /* end function fillDeck */

/* shuffle cards */
void shuffle( Card * const wDeck )
{
int i; /* counter */
int j; /* variable to hold random value between 0 - 51 */
Card temp; /* define temporary structure for swapping Cards */

/* loop through wDeck randomly swapping Cards */


for ( i = 0; i <= 51; i++ ) {
j = rand() % 52;
temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
} /* end for */

} /* end function shuffle */

/* deal cards */
void deal( const Card * const wDeck )
{
int i; /* counter */

/* loop through wDeck */


for ( i = 0; i <= 51; i++ ) {
printf( "%5s of %-8s%c", wDeck[ i ].face, wDeck[ i ].suit,
( i + 1 ) % 2 ? '\t' : '\n' );
} /* end for */
}
8-/* file: struct.c The following program demonstrates usage of structure */
#include <stdio.h>
typedef struct
{
int p, q, rotating ;
}moon;
moon google (moon cps125, int x, int *z){
cps125.p= x + 35;
while (x >= 0)
{
cps125.q = *z + x;

11
x = x - 1;
}
return (cps125);
}
int main (void)
{
moon venus;
int tt=43, dd=58 ;
venus = google (venus, tt, &dd);
while(tt<100){
printf ("%d %d\n", venus.p,venus.q);
return(0);
}}
9-/* file: array_2d_2.c The following program demonstrates multiplication of a
matrix and a scalar array .*/
#include <stdio.h>
#define COLS 3
#define ROWS 4
int
main (void)
{
int i,k;
int vec[3]={1,2,2};
int mat[4][3]={{1,1,1},{2,3,1},{1,-1,-1},{0,1,2}};
int prod[4];
/* do the multiplication */
for (i=0; i<ROWS; ++i)
{
prod[i] = 0;
for (k=0; k<COLS; ++k)
prod[i] = prod[i] + mat[i][k] * vec[k];
}
/* display vector */
printf ("The vector is: \n----\n");
for (i=0; i<COLS; ++i)
printf ("%d\n", vec[i]);
printf ("----\n");
/* display matrix */
printf ("The matrix is:\n");
for (i=0; i<ROWS; ++i)
{
for (k=0; k<COLS; ++k)
printf ("%d\t", mat[i][k]);
printf ("\n");
}
printf ("\n");

12
/* display product vector */
printf ("The product is: <");
for (i=0; i<ROWS; ++i)
printf ("%d, ", prod[i]);
printf (">\n");
return (0);
}
10-/* file: array_2d_3.c The following program demonstrates simple
multiplication of a 2 dimensional arrays,
***********************************************
**** ONLY simple cellA[x][y] * cellB[x][y] ****
*********************************************** */
#include <stdio.h>
#define M 4 /* n of rows in first matrix */
#define N 4 /* n of cols in first and of rows in 2nd */
#define P 5 /* n of cols on 2nd matrix */
void multmatrix (int a[M][N], int b[M][N], int c[M][N])
{
int i,j;
for (i=0; i<M; ++i)
for (j=0; j<N; ++j)
{
c[i][j] = a[i][j] * b[i][j];
}
}
int main (void)
{
int i,j;
int mat1[M][N]={{1,2,2,2},{2,-3,6,4},{8,1,0,-3}, {3, 6, -1, 0}};
int mat2[M][N]={{1,1,1,0},{2,3,1,6},{1,-1,-1,8},{0,1,2,3}};
int matprod[M][N];
/* do the multiplication */
printf("Matrix 1\n");
for (i=0; i<M; ++i)
{
for (j=0; j<N; ++j)
printf ("%4d ", mat1[i][j]);
printf ("\n");
}
printf("\nMatrix 2\n");
for (i=0; i<M; ++i)
{
for (j=0; j<N; ++j)
printf ("%4d ", mat2[i][j]);
printf ("\n");
}

13
multmatrix (mat1, mat2, matprod);
/* display the resulting matrix */
printf("\nFinal Matrix\n");
for (i=0; i<M; ++i)
{
for (j=0; j<N; ++j)
printf ("%4d ", matprod[i][j]);
printf ("\n");
}
return (0);
}
11-/* file: array_2d_dyn1.c The following program demonstrates dynamically
created 2 dimensional arrays */
#include <stdio.h>
#include <stdlib.h>
void show_2d_array(int **array, int i, int j){
int x = 0, y = 0;
printf("\n==============\nDisplaying matrix [%d][%d]\n", i, j);
for (x = 0; x < i; x++)
{
for (y = 0; y < j; y++)
{
printf("%4d", array[x][y]);
}
printf("\n");
}
printf("Done Display\n\n");
}
int main (void)
{
int nrows, ncols, i, j;
int **numbers; /* pointer to the first cell ([0][0]) */
printf ("How many rows and columns?> ");
scanf ("%d%d", &nrows, &ncols);
/* allocating the array of pointers */
numbers = (int **) calloc (nrows, sizeof(int *));

/* allocating the array of integers */


for (i=0; i<nrows; ++i)
numbers[i] = (int *) calloc (ncols, sizeof(int));
i=1; j=1;
numbers[i][j] = 9; /* initializes one value to 9 */
for (i=0; i<nrows; i=i+1)
{
for (j=0; j<ncols; j=j+1)
{

14
printf ("%3d ", numbers[i][j]);

}
printf ("\n");
}
show_2d_array(numbers, nrows, ncols);
/* freeing the array */
for (i=0; i<nrows; ++i)
free (numbers[i]);
free (numbers);
return (0);
}
12-/* file: recursion.c The following program demonstrates recursive functions. */
#include <stdio.h>
int fact(int x);
int summed(int x);
int main(void)
{
int one;
printf("\nEnter a positive numbers: ");
scanf("%d", &one);
while (one > 0){
printf("fact(%d) = %d\n", one, fact(one));
printf("summed(%d) = %d\n", one, summed(one));
printf("\nEnter a positive numbers: ");
scanf("%d", &one);
}

return (0);
}
/* return factorial of x, recursively */
int fact(int x)
{
int res;
if (x == 0)
{
res = 1;
}
else
{
res = x * fact(x - 1);
}
return res;
}
/* return sum of numbers in range [x, 0], recursively */
int summed(int x)

15
{
int res;
if (x == 0)
{
res = 0;
}
else
{
res = x + summed(x - 1);
}
return res;
}

}
MAIN WEBPAGE SAMPLES

1) Program a function that takes a string of lowercase letters and return the same
string with lowercase "a" changed into an uppercase "A" and any combination of
"to" changed into "TO" (do not change t and o alone). The function prototype is
void q1 (char string[ ]);

void
q1 (char string[])
{
int i=0;
while (string[i]!='\0')
{
if (string[i]=='a')
string[i] = 'A';
if (string[i]=='t' && string[i+1]=='o')
{
string[i] = 'T';
string[i+1] = 'O';
}
i=i+1;
}
}

2) Write a complete program reading from a file numbers (the first number being
the quantity of the other numbers to read). Place these last numbers in an array.
All numbers are integers. Write a function taking this array as input and returning
the largest number as output. The function prototype will be
int largest (int array[ ], int size);

#include <stdio.h>

16
int
largest (int array[], int size)
{
int i;
int larget;
largest = array[0];
for (i=0; i<size; +i)
if (array[i]>large)
large=array[i];
return (large);
}

int
main (void)
{
int n, array[100], i, big;
FILE *in;
in = fopen ("file.txt", "r");
fscanf (in, "%d", &n;);
for (i=0; i<n; +i)
fscanf (in, "%d", &array[i]);
big = largest (array, n);
printf ("The largest number is %d", big);
fclose (in);
return (0);
}

3) We have a program that has three parallel arrays: barcode (an array of
integers), description (an array of strings) and price (an array of doubles). The
maximum size of the arrays is 100. The program will display the price and
description on when you enter a specific barcode. Write a function only that will
send back a price and a description to the main when the barcode is sent to it.
The function prototype will be
double scanner (char descr[100][30], double price[100], int barcode[100],
int scancode, int* location);

double
scanner (char descr[100][30], double price[100], int barcode[100], int scancode, int*
location)
{
int i;
for (i=0; i<100; +i)
if (barcode[i]==scancode)
*location = i;
}

17
4) Write a function using structure types which when given today's date in this
format mm dd yy will return out yesterday's date. The structure is typedef struct
date { int mm,dd,yy;} date; and the function prototype is
date yesterday (date today);

date
yesterday (date today)
{
date y;
y.dd = today.dd-1;
y.mm = today.mm;
y.yy = today.yy;
if (y.dd==0)
{
y.mm=y.mm-1;
if
(y.mm==1||y.mm==3||y.mm==5||y.mm==7||y.mm==8||y.mm==10||y.mm==12)
y.dd=31;
if (y.mm==4||y.mm==6||y.mm==9||y.mm==11)
y.dd=30;
if (y.mm=2)
y.dd=28;
if (y.mm==0)
{
y.mm=1;
y.yy=y.yy-1
}
}
return (y);
}

5) Write a complete program that asks a user for a sentence and converts all
uppercase characters into lowercase characters and leaves all non-uppercase
characters unchanged. Display the changed sentence.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int
main (void)
{
char sentence[200];
int i=0;
gets (sentence);
while (sentence[i]!='\0')
{

18
sentence[i] = tolower (sentence[i]);
i=i+1;
}
printf ("%s", sentence);
return (0);
}

6) Write a complete program that fills an array 5x6 of integers from a file and
prints out the numbers in the main diagonal.

#include <stdio.h>
int
main (void)
{
int a[5][6], i, j;
FILE *in;
in = fopen ("file.txt", "r");
for (i=0; i<5; ++i)
for (j=0; j<6; ++j)
fscanf (in, "%d", &a[i][j]);
fclose (in);
for (i=0; i<5; ++i)
printf ("%d ", a[i][i]);
return (0);
}

7) Write a complete program that does these actions:

• asks the user for the the name of a data file and opens that file.
• asks the user for how many numbers to expect in the file (integers).
• allocates a dynamic array of exactly the size required.
• displays the numbers present in the even cells [0],[2],[4],....
#include <stdio.h>
#include <stdlib.h>
int
main (void)
{
char filename;
int n, *array, i;
FILE *in;
printf ("File name: ");
scanf {"%s", filename);
in = fopen (filename, "r");
printf ("How many numbers? ");
scanf ("%d", &n);
array = (int *) calloc (n, sizeof (int));

19
for (i=0; i<n, ++i)
{
fscanf (in, "%d", &array[i]);
if (i%2==0)
printf ("%d ", array[i]);
}
fclose (in);
free (array);
return (0);
}

8) Write a complete program that asks the user for the size of a matrix (rows and
columns) and allocates dynamically that matrix as a 2D array. Have the
matrix/array filled from the keyboard by the user. It is an array of characters.
Finally have the program display the top-right corner character in the array. If the
5x2 array is

t y e h s
f h e t g
the program should display s.
#include <stdio.h>
#include <stdlib.h>
int
main (void)
{
int rows, cols;
char *matrix;
printf ("Enter the number of rows: ");
scanf ("%d", &rows);
printf ("Enter the number of columns: ");
scanf ("%d", &cols);
matrix = (char *) calloc (rows*cols, sizeof(char));
for (i=0; i<rows; ++i)
for (j=0; j<cols; ++j)
scanf ("%c", &matrix[i*cols+j];
printf ("Top-right corner character is: %c", matrix[0*cols+(cols-1)]);
free (matrix);
return (0);
}

9) Write a complete program that will accept a command line argument


consisting of a sentence and will display how many words in the sentence. In this
example, the program is called pgm:
The command line is pgm The quick brown fox.
The program will display The sentence has 4 words.

#include <stdio.h>

20
int
main (int argc, char *argv[])
{
printf ("The sentence has %d words.\n", argc-1);
return (0);
}

10) Write a complete program that will read the following data from a file called
tv.dat. The format is the following: the first data is a integer indicating how many
words will follow. Here is a typical file:

3 my three sons
2 star trek
1 friends
2 home improvement
1 lost
4 hockey night in canada
2 the national
Your program must display all the tv show titles that have 3 words or more.
#include <stdio.h>
int
main (void)
{
int nw, i;
FILE *in;
in = fopen ("tv.dat", "r");
while (fscanf (in, "%d", &nw)!= EOF)
{
for (i=0; i<nw; ++i)
{
fscanf (in, "%s", word);
if (nw>=3)
printf ("%s", word);
}
}
fclose (in);
return (0);
}

21

Você também pode gostar