Answer to Question #111250 in C for sara

Question #111250
Use matrix subtraction to calculate how much longer was the life expectancy of black
and white women than men of the same races in each decade from year 1950 to year
2000. There is no need to store the year data in your matrices—just provide the years in
the output.
a) Input each matrix from user
b) Calculate the matrix difference by calling a function you write called matrix_diff that
will subtract any two 6-by-2 matrices, producing a third 6-by-2 matrix.
c) Display all three matrices with appropriate labels. Your function should calculate the
matrix difference by subtracting each element of the second matrix from the
corresponding element of the first.
United States Life Expectancy at Birth by Sex and Race
1
Expert's answer
2020-04-22T09:07:54-0400
#include <stdio.h>
#define ROW 2
#define COL 6
void matrix_diff(double[][COL], double[][COL], double[][COL]);
void display(double[][COL]);
int main()
{
    FILE *male = fopen("male.txt", "r"), *female = fopen("female.txt", "r");
    double MALE_MAT[ROW][COL], FEMALE_MAT[ROW][COL], DIFF_MAT[ROW][COL];
    int i, j;

    while(!feof(male))
    {
        for(i = 0; i < ROW; i++)
            for(j = 0; j < COL; j++)
                fscanf(male, "%lf", &MALE_MAT[i][j]);
    }

    while(!feof(female))
    {
        for(i = 0; i < ROW; i++)
            for(j = 0; j < COL; j++)
                fscanf(female, "%lf", &FEMALE_MAT[i][j]);
    }

    matrix_diff(MALE_MAT, FEMALE_MAT, DIFF_MAT);

    printf("\nMale");
    display(MALE_MAT);

    printf("\n\nFemale");
    display(FEMALE_MAT);

    matrix_diff(MALE_MAT, MALE_MAT, DIFF_MAT);

    printf("\nDifference");
    display(FEMALE_MAT);

    fclose(male);
    fclose(female);
}

void matrix_diff(double A[][COL], double B[][COL], double C[][COL])
{
    int i, j;
    for(i = 0; i < ROW; i++)
    {
        for(j = 0; j < COL; j++)
        {
            C[i][j] = A[i][j] - B[i][j];
        }
    }
}

void display(double A[][COL])
{
    int i, j, year = 1950;
    printf("\nYear %3c Black %3c White\n", ' ', ' ');
    for(i = 0; i < COL; i++)
    {
        printf("\n%d %2c", year, ' ');
        for(j = 0; j < ROW; j++)
        {
            printf("%.1f  ", A[i][j]);
        }
        year += 10;
    }
}


This program reads files:

  1. female.txt
62.9 72.2
66.3 74.1
68.3 75.6
72.5 78.1
73.6 79.4
75.2 80.1

2.male.txt

59.1 66.5
61.1 67.4
60.0 68.0
63.8 70.7
64.5 72.7
68.3 74.9

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

sara
22.04.20, 16:41

great man

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS