Friday, November 13, 2020

C Program Assignments and Answers 2

Question 

Write a menu driven program in C to process the sales data in branches (Use arrays).
a) A function to input number of cars sold in each branch.
b) A function to display the highest and the lowest number of cars sold.
c) A function to display the total number of cars sold.
d) A function to display the number of cars sold in each branch.
e) A function which display the no. Of branches which have achieved the monthly target.



Answer
 #include <studio.h>
#define MAX 20
#define TARGET 15

void InputSale(char c[], float s[]);
void printSale(char c[],float s[]);
void printComp(char c[]);
void printLowHigh(float s[]);
void printTotSale(float s[]);
void printTarget(float s[]);

main()
{
    int T;
    char choice;
    T = 1;
    char comp[MAX];
    float sales[MAX];

    while (T != 0)
    {
        printf("%s\n", "1 -Input Branch and Sales ");
        printf("%s\n", "2 -Display the highest and the lowest number of cars sold ");
        printf("%s\n", "3 -Display the total number of cars sold. ");
        printf("%s\n", "4 -Display the number of cars sold in each branch. ");
        printf("%s\n", "5- Display the no. Of branches which  achieved  target");
        printf("%s\n", "6 -Display Branches");
        printf("%s\n", "7 -Exit ");
        printf("%s\n", "Enter a Selection 1 -7 ?");
        scanf_s(" %c", &choice);
        switch (choice)
        {
        case '1':
            InputSale(comp, sales);
            break;
        case '2':
            printLowHigh(sales);
            break;
        case '3':
            printTotSale(sales);
            break;
        case '4':
            printSale(comp, sales);
            break;
        case '5':
            printTarget(sales);
            break;
        case '6':
            printComp(comp);
            break;
        case '7':
            T = 0;
            break;
        default:
            printf("%s", "invalid selection please enter correct selection");
            break;
        }
    }  
    getch();
}

void InputSale(char c[], float s[])
{
    int i;
    for ( i = 0; i < MAX; ++i)
    {
        printf("%s", "Enter Branch ?");
        scanf_s(" %c", &c[i]);
        printf("%s", "Enter Sales ?");
        scanf_s("%f", &s[i]);

    }
}
void printSale(char c[],float s[])
{
    int i;
    for (i = 0; i < MAX; ++i)
    {
        printf(" %c - %f\n", c[i],s[i]);
    }
}

void printTotSale(float s[])
{
    int i;
    float Tot = 0;
    for (i = 0; i < MAX; ++i)
    {
        Tot = Tot + s[i];
    }
    printf("Total Sales %f \n", Tot);
}

void printTarget(float s[])
{
    int i;
    int C = 0;
    for (i = 0; i < MAX; ++i)
    {
        if (s[i] > TARGET)
            C = C + 1;
    }
    printf("Number of Companies Acheived Target %d \n", C);
}

void printComp(char c[])
{
    int i;
    for (i = 0; i < MAX; ++i)
    {
        printf("%c\n", c[i]);
    }
}
void printLowHigh(float s[])
{
    int i;
    float L, H;
    L = s[0];
    H = s[0];
    for (i = 1; i < MAX; ++i)
    {
        if (s[i] > H)
            H = s[i];
        if (s[i] < L)
            L = s[i];
    }
    printf("Highest sales Value is %f\n", H);
    printf("Lowestest sales Value is %f\n",L);
}

No comments:

Post a Comment