Question

In: Computer Science

C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...

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!

Solutions

Expert Solution

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:-


Related Solutions

REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] );...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] ); Argument pt[2] defines a point on the plane: pt[0] is the x-coordinate, pt[1] is the y-coordinate. Argument rect[4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle. rect[2] and rect[3] define the opposite corner. Coordinates may be any valid floating point value, including negative values. The function returns int 0...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT