Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(printInput.c) Write a program that takes a variable number of values as strings

ID: 3759233 • Letter: #

Question

(printInput.c) Write a program that takes a variable number of values as strings from the command line. Your program must implement the following functions. (Hint: use a for loop with argc in the terminating condition to take in a variable number of values)


a) int getDataType(char *num)
b) void printValuePlusTen(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)


printValuePlusTen will convert the string num into an integer or floating point number by using the atoi or atof functions. It will then add ten to the number and print it out.


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 plus ten using the two functions above.


Sample output screenshot:

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<string.h>

int getDataType(char *num)
{
   int i;
   for(i=0;i<strlen(num);i++)
   {
       //printf("%c",num[i]);
       if(num[i]=='.')
       {
           return 2;
       }
   }
   return 1;
}

void printValuePlusTen(char *num, int no)
{
   int n=0,n1;
   float nn,n2;
   if(no==1)
   {
       n=atoi(num);
       n1=n+10;
       printf(" %d",n1);
   }
   else
   {
       nn=atof(num);
       //printf(" %f",nn);
       n2=nn+10.0;
       printf(" %f",n2);
   }
}
void main(int argc, char* argv[])
{
   int i,no;

   for(i=1;i<argc;i++)
   {
       no=getDataType(argv[i]);
       printValuePlusTen(argv[i], no);
   }
   getch();
}