Write a C program that takes 3 values as strings from the command line. Your pro
ID: 3567502 • Letter: W
Question
Write a C program that takes 3 values as strings from the command line. Your program must implement the following functions.
a) int getDataType(char *num)
b) void printValue(char *num, int dataType)
getDataType will take a string as input and return 1 if the number is an int and 2 if the number is a double. (hint: check for a
Write a C program that takes 3 values as strings from the command line. Your program must implement the following functions. a) int getDataType(char *num) b) void printValue(char *num, int dataType) getDataType will take a string as input and return 1 if the number is an int and 2 if the number is a double. (hint: check for a ?????.????? in the string) printValue takes the pointer to character array and data type as input and uses either the atoi or atof function to print out the value. Your main function should loop through the input values in argv where on each value it will check the data type and print out the value using the two functions above. Sample output screenshot:Explanation / Answer
#include <stdio.h>
#include <string.h>
int getDataType(char *num){
int i;
for(i = 0; i < strlen(num); i++){
if(num[i] == '.') return 2;
}
return 1;
}
void printValue(char *num, int dataType){
if(dataType == 1) printf("%d ", atoi(num));
else if(dataType == 2) printf("%f ", atof(num));
}
int main(int argc, char *argv[]){
int i;
for(i = 1; i <= 3; i++){
printf("argv[%d] = ", i);
printValue(argv[i], getDataType(argv[1]));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.