2d Array Explained Perfectly

Vid:

code:

#include<stdio.h>
int main()
{
    int x[4][3];
    int i,j;
    for(i=0; i<4; i++)
    {
        for(j=0; j<3; j++)
        {
            x[i][j]=i*j;
        }
    }
    //printf("%d",i);
    printf("content of the 2d array:\n");
    for(i=0; i<4; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%3d",x[i][j]); //format specifier width selector
        }
        printf("\n");
    }

    return 0;
}

Another Good Example:

Code:

#include<stdio.h>
int main()
{
    int r,c,i,j;
    printf("Enter number of sales person:");
    scanf("%d",&r);
    printf("\nEnter sold number of items:");
    scanf("%d",&c);
    printf("\n\n");

//Take input from user
    printf("enter sales data:\n");
    double sale[r][c];
    for(i=0; i<r; i++)
    {
        for(j=0; j<c; j++)
        {
            printf("\nEnter data for sale peron no %d and item no %d:",i+1,j+1);
            scanf("%lf",&sale[i][j]);
        }
    }

//display data
    printf("\n\n");
    printf("\n\n");
    printf("Sales Data Is Here\n");

    double total=0.0;
    double comm;
    for(i=0; i<r; i++)
    {
        printf("---------------------------------\n");
        printf("Sales data for sales person %d\n",i+1);
        printf("---------------------------------\n");
        for(j=0; j<c; j++)
        {

            printf("\nItem %d = $%.2lf",j+1,sale[i][j]);
            total=total+sale[i][j];
            comm=total*.10;
        }
        printf("\nTotal sale: %.2lf\n",total);
        printf("Total comm for sale peron %d: %.2lf\n\n",i+1,comm);
        total=0.0;
    }


    return 0;
}

 

Output:

Enter number of sales person:3

Enter sold number of items:2


enter sales data:

Enter data for sale peron no 1 and item no 1:1.75

Enter data for sale peron no 1 and item no 2:4.5

Enter data for sale peron no 2 and item no 1:3.6

Enter data for sale peron no 2 and item no 2:10.95

Enter data for sale peron no 3 and item no 1:5.5

Enter data for sale peron no 3 and item no 2:2.25




Sales Data Is Here
---------------------------------
Sales data for sales person 1
---------------------------------

Item 1 = $1.75
Item 2 = $4.50
Total sale: 6.25
Total comm for sale peron 1: 0.63

---------------------------------
Sales data for sales person 2
---------------------------------

Item 1 = $3.60
Item 2 = $10.95
Total sale: 14.55
Total comm for sale peron 2: 1.46

---------------------------------
Sales data for sales person 3
---------------------------------

Item 1 = $5.50
Item 2 = $2.25
Total sale: 7.75
Total comm for sale peron 3: 0.78


Process returned 0 (0x0)   execution time : 17.786 s
Press any key to continue.

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.