In: Computer Science
C Practice 1:
1) Write a forward declaration for the following C
function
int triple_it (int x) {
return (x * 3);
}
2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers?
3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array.
4) Write a C function letter_count(char a[], int len) that takes a string & length as a parameter and returns the number of letters (both uppercase and lowercase) in the string. Keep in mind that characters in C are integers, so you can do comparisons such as ((c >= 'A') && (c <= 'Z')) to check if a character is an uppercase character.
5) Write a function called printThem(int count) which print the
numbers from 1 to n with the following conditions:
- In place of multiples of 3 print three.
- In place of multiples of 5, print five.
- If a number is divisible by both 3 and 5 print fifteen.
- Do not print multiples of 7 (even if a multiple of 3)
Display 15 numbers per line. Include sample output for n=50
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please include comments!
Answer:
1) The forward declaration of C function:
int triple_it (int x) ; /*It is also called function prototype*/
2) C syntax to declare two variables:
int num; /* num of integer type */
float farray[10]; /* farray array of 10 floating point numbers */
3)
/*This function takes an integer array & its length as its parameters and returns the largest value in the array.*/
int array_max(int a[], int len)
{
int largest; /*variable to store largest number*/
int i;
/*Assuming that first element of array is largest number*/
largest = a[0];
for(i=1; i<len; i++)
{ /* Comparing largest variable with every element
of array. If there is an element which is greater than
largest variable value then that element of array will be stored
in largest variable. In this way at the end of loop we will get
the largest value in variable latgest.
*/
if(a[i]>largest)
largest = a[i];
}
return largest;
}
Sample program to test the above function:
#include <stdio.h>
/*Function prototype*/
int array_max(int a[], int len);
int main()
{
int a[] = {10, 100, 17, 202, 150};
int largest;
largest = array_max(a, 5);
printf("The largest number is %d\n", largest);
}
/* This function takes an integer array & its length as its parameters and returns the largest value in the array.*/
int array_max(int a[], int len)
{
int largest; /*variable to store largest number*/
int i;
/*Assuming that first element of array is largest number*/
largest = a[0];
for(i=1; i<len; i++)
{ /* Comparing largest variable with every element
of array. If there is an element which is greater than
largest variable value then that element of array will be stored
in largest variable. In this way at the end of loop we will get
the largest value in variable latgest.
*/
if(a[i]>largest)
largest = a[i];
}
return largest;
}
Sample output:-
4)
/*This function takes a string & length as a parameter and returns the number of letters */
int letter_count(char a[], int len)
{
/*variable to store number of lowercase and uppercase
letters*/
int letters=0;
int i;
for(i=0; i<len; i++)
{ /*Checking for lowercase or uppercase
letters*/
if( ((a[i] >= 'A') &&
(a[i] <= 'Z')) || ((a[i] >= 'a') && (a[i] <= 'z'))
)
letters++;
}
return letters; /*Returns number of
letters*/
}
Sample program to test the above function:
#include <stdio.h>
#include <string.h>
/*Function prototype*/
int letter_count(char a[], int len);
int main()
{
char a[] = "Hello World";
int letters;
int len;
/*Calculating the length of string*/
len=strlen(a);
/*Calling function to get the number of letters*/
letters = letter_count(a, len);
printf("The number of letters in string is %d\n", letters);
}
/*This function takes a string & length as a parameter and returns the number of letters */
int letter_count(char a[], int len)
{
/*variable to store number of lowercase and uppercase letters*/
int letters=0;
int i;
for(i=0; i<len; i++)
{ /*Checking for lowercase or uppercase letters*/
if( ((a[i] >= 'A') && (a[i] <= 'Z')) || ((a[i] >= 'a') && (a[i] <= 'z')) )
letters++;
}
return letters; /*Returns number of letters*/
}
Sample output:-
5)
/*This function print the numbers from 1 to n with the following conditions:
- In place of multiples of 3 print three.
- In place of multiples of 5, print five.
- If a number is divisible by both 3 and 5 print fifteen.
- Do not print multiples of 7 (even if a multiple of 3)
Display 15 numbers per line.
*/
void printThem(int count)
{
int i;
int line=1; /*Variable to keep track of number printed per line*/
for(i=1; i<=count; i++)
{ /*Checking condition not to print multiples of 7 (even if a multiple of 3)*/
if( i%7 != 0 )
{ /*Condition if a number is divisible by both 3 and 5 print fifteen.*/
if( i%3 == 0 && i%5 == 0 )
printf("fifteen ");
else
/*Checking condition to print three in place of multiples of 3.*/
if( i%3 == 0)
printf("three ");
else
/*Checking condition to print five in place of multiples of 5.*/
if( i%5 == 0 )
printf("five ");
else
/*Otherwise print number in digit form*/
printf("%d ", i);
line++;
}
/*When value of variable line become more than 15 then change the line.
Variable line is used to print 15 numbers per line*/
if(line>15)
{
line=1;
printf("\n"); /*new line*/
}
}
}
Sample program to test the above function:
#include <stdio.h>
/*Function prototype*/
void printThem(int count);
int main()
{
printThem(50); /*Calling function to print number 1 to 50 */
}
/*This function print the numbers from 1 to n with the following conditions:
- In place of multiples of 3 print three.
- In place of multiples of 5, print five.
- If a number is divisible by both 3 and 5 print fifteen.
- Do not print multiples of 7 (even if a multiple of 3)
Display 15 numbers per line.
*/
void printThem(int count)
{
int i;
int line=1; /*Variable to keep track of number printed per line*/
for(i=1; i<=count; i++)
{ /*Checking condition not to print multiples of 7 (even if a multiple of 3)*/
if( i%7 != 0 )
{ /*Condition if a number is divisible by both 3 and 5 print fifteen.*/
if( i%3 == 0 && i%5 == 0 )
printf("fifteen ");
else
/*Checking condition to print three in place of multiples of 3.*/
if( i%3 == 0)
printf("three ");
else
/*Checking condition to print five in place of multiples of 5.*/
if( i%5 == 0 )
printf("five ");
else
/*Otherwise print number in digit form*/
printf("%d ", i);
line++;
}
/*When value of variable line become more than 15 then change the line.
Variable line is used to print 15 numbers per line*/
if(line>15)
{
line=1;
printf("\n"); /*new line*/
}
}
}
Sample output:-