Question

In: Computer Science

C Programming question: Develop a program named “sample” that prints the value of two functions: 1....

C Programming question:

Develop a program named “sample” that prints the value of two functions:
1. Function f1 takes a single input argument of type int and returns the value of its input argument as float
2. Function f2 takes a single input argument of type char and returns an int. The output of f2 is the result of the sum of the values of the global variables, and the input
argument minus the value of char ‘a’ plus the value of char ‘A’

Structural Requirements:
1. Your executable must be named sample (your makefile must ensure this)
2. Your program should not take any arguments
3. Your solution must have at least two C source files (e.g., a main and file1.c), two header files (e.g., globals.h, and externs.h), and a makefile
4. Your program should define two global variables (e.g., p and q), both of type int, one with value 11 and the second with a value of 32
5. Your main should have two local variables: int i = 5; char c = ‘x’
6. Main should invoke f1 with the sum of i, p, and q as input, and pass the value of c to f2

Solutions

Expert Solution

IDE Used: Dev C++

Required Files are given below

main.c

#include <stdio.h>
#include <stdlib.h>
#include "globals.h" //for using function f1 and f2
#include "externs.h" //for using global variables p and q   

{
   int i =5;
   char c='x';
  
   //sum of i+p+q
   i=i+p+q;
  
   //calling function f1
   printf("\nResult of function f1: %0.2f",f1(i));
  
   //calling of function f2
   printf("\nResult of function f2: %d",f2(c));
   return 0;
}

file.c

#include <stdio.h>
#include <stdlib.h>
#include "globals.h" //to use global variables p and q


extern int p=11;
extern int q=32;

//defination of function f1
float f1(int i)
{
   return (float)i;
}

//defination of function f2
int f2(char c)
{
   return (int)c+p+q-'a'+'A';
}

externs.h

#ifndef file1
#define file1

//declaration of function f1 and f2
float f1(int);
int f2(char);

#endif

globals.h

#ifndef globalVar

//declaration of global variales
int p;
int q;

#endif

Makefile.win

# Project: sample
# Makefile created by Dev-C++ 5.11

CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = main.o file1.o
LINKOBJ = main.o file1.o
LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN = sample.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
   ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
   $(CC) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.c
   $(CC) -c main.c -o main.o $(CFLAGS)

file1.o: file1.c
   $(CC) -c file1.c -o file1.o $(CFLAGS)

Output


Related Solutions

Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
C# Programming using Windows Form 1. Define a variable named isTrue that stores a value of...
C# Programming using Windows Form 1. Define a variable named isTrue that stores a value of whether something is true or false. Set this variable    to the negative. Output this variable to the txtIsTrue textbox. 2. Define a variable named favoriteGame that could store the name of a game. Set this variable's value to your favorite game. Output this variable to the txtFavoriteGame textbox. 3.. Define a variable named pi that can store real numbers. initialize it to a value...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
This is C# programming. In Chapter 2, you created an interactive application named GreenvilleRevenue. The program...
This is C# programming. In Chapter 2, you created an interactive application named GreenvilleRevenue. The program prompts a user for the number of contestants entered in this year’s and last year’s Greenville Idol competition, and then it displays the revenue expected for this year’s competition if each contestant pays a $25 entrance fee. The programs also display a statement that compares the number of contestants each year. Now, replace that statement with one of the following messages: If the competition...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT