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

l. Write a simple C program to take 3 parameters on the command line, make sure

ID: 3785479 • Letter: L

Question

l. Write a simple C program to take 3 parameters on the command line, make sure they are numbers and convert them to int type, then sum them and display the result like: The sum of 4 7 6 is 17. where the underlined numbers are the three parameters. If any of the parameters are entered incorrectly, display an error message. Either way, terminate when done. (8 pts.) 2. Create a makefile for the above program. Make sure the makefile has targets to build the program, build a debug version, and clean the project. (2 pts.) 3. Modify the program from (1) to only accept integers from 0 to 50 inclusive. (2 pts.) 4. Modify the program from (1) to use floating point numbers. (2 pts.) 5. Modify the program from (3) to accept a 4th and 5th parameter which specifies the operations to use between the 1st and 2nd parameter and the 2nd and 3rd parameter respectively. For example, "3 6 2+ l'' would calculate 3 6/2 (order of precedence is left to right, so (3 6) 2 4). Change the output to display the operations and display an error ifthe last two parameters have anything other than or (Hint: a switch statement is probably best for handling the last two parameters.) (6 pts.)

Explanation / Answer

1)


#include<strings.h>
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
    int sum;
   char s[1000];

fgets(s, sizeof(s), stdin);
int valid = 1,i;
for (i = 0; i < strlen(s); ++i)
{
   if (!isdigit(s[i]))
    {
        valid = 0;
        break;
    }
}
if(valid==0) {
    s[i]=(int)s[i];
    valid=1;
    i++;
    //goto l1;
}
for(int i=0;i<strlen(s);i++){
    sum=sum+s[i];
}
printf("%d",sum);
}

Answer
3)


#include<strings.h>
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
    float sum;
   char s[1000];

fgets(s, sizeof(s), stdin);
int valid = 1,i;
for (i = 0; i < strlen(s); ++i)
{
   if (!isdigit(s[i]))
    {
        valid = 0;
        break;
    }
    int isInteger(double val)

if(valid==0) {
    s[i]=(float)s[i];
    valid=1;
    i++;
    //goto l1;
}
for(int i=0;i<strlen(s);i++){
    sum=sum+s[i];
}
printf("%f",sum);
}

Answer
4)

#include<stdio.h>
#include<ctype.h
int st[20];
int top = -1;

void push(int n)
{
        st[++top] = n;
}

int pop()
{
        return st[top--];
}

int main()
{
        char ex[20];
        char *e;
        int n1,n2,n3,ans;
     
        scanf("%s",ex);
        e = ex;
        while(*e != '')
        {
                if(isdigit(*e))
                {
                        ans = *e - 48;
                        push(ans);
                }
                else
                {
                        n1 = pop();
                        n2 = pop();
                        switch(*e)
                        {
                                case '+':
                                {
                                        n3 = n1 + n2;
                   break;
                                }
                                case '-':
                                {
                                        n3 = n2 - n1;
                                        break;
                                }
                                case '*':
                                {
                                        n3 = n1 * n2;
                                        break;
                                }
                                case '/':
                                {
                                        n3 = n2 / n1;
                                        break;
                                }
                        }
                        push(n3);
                }
                e++;
        }
        printf(" The result of expression %s = %d ",ex,pop());
        return 0;

}