Thread: 2Dimensional Arrays-find lowest, largest, and total of each column HELP!!!!

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation 2Dimensional Arrays-find lowest, largest, and total of each column HELP!!!!

    Hey everyone!
    So i posted about this assignment earlier, but i had a completely different problem, so I decided to make a new thread!

    So, here is the assignment:

    Code:
    Project: Sales Tables
     
    The following table shows the total sales for salespeople of the ABC Manufacturing Company.
            Salesperson      Week 1  Week 2 Week 3  Week 4
        ===============  ======  ======  ======  ======
        Nguyen, Michael    25     30      45      20
        Johnson, Mary      32     30      33      29
           ...                    
    The price of the product being sold is $1985.95 (define it as a constant). 
    Write a program that permits the input of the data, up to 25 salespersons and up to 6 weeks, 
    prints a replica of the original table to the screen, and 
    writes to a file a table showing the dollar value of sales for each individual during each week along with his or her total sales. 
    Also it prints the total sales, the lowest and the highest value for each week, 
    and the total sales for the company.
    So, i have 4 functions right now: main, getNames, getSales, and calcSales.

    I'm trying to write a 5th function that will calculate the lowest and highest from each column, the total for each week, and the total for each person. Somehow, my function wont work. I tried to use a debugger but I cant find the problem..it just seems that I'm not doing my for loops correctly or something...could u please take a look at my code? THANKS!!!

    here's my code:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define PRICE_OF_PRODUCT 1985.95
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    int getSales(int salesArray [][MAX_COLS_SALES], int a);
    void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES]);//, double sum[MAX_ROWS_SALES]);
    double calcHiLowTotal(int size, int numWeeks, double calc[][MAX_COLS_SALES], double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES], double high[MAX_ROWS_SALES]);
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
    
       double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       //double sum[MAX_ROWS_SALES] = {0};
       double calc[][MAX_COLS_SALES] = {0};
       double total_sales[MAX_ROWS_SALES] = {0};
       double low[MAX_ROWS_SALES] = {0};
       double high[MAX_ROWS_SALES] = {0};
    
       int i = 0;
       int j = 0;
       int row;
       int col;
       int numNames=0;
       int numWeeks=0;
      //Statements
    
       printf("*** Salespersons *** \n");
    
       numNames=getNames (namesArray);
       numWeeks=getSales (salesArray, numNames);
       calcSales(numNames, numWeeks, salesArray, total);//, sum);
       calcHiLowTotal (numNames, numWeeks, calc, total_sales, low, high);
    
    
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    int a;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
    
    
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
           fscanf(fp, "%d", &a);
        if (a <= MAX_COLS_NAMES)
        {
    
    
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
                {
    
                fscanf(fp, "%c", &namesArray[i][j]);
                printf("%c", namesArray[i][j]);
         }
         printf("\n");
     }
        }
        else
            printf(" ***Warning: data file is too large *** \n");
    
    
    
    
    fclose(fp);
    
    return a;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES], int a)
    {
        //Statements
    FILE *fl;
    int i=0, j=0, c, b;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
        if(fl == NULL)
        {
            printf("Error, can't open file!!!\n");
            exit(101);
        }
    
        fscanf(fl, "%d", &b);
    
        if (b < MAX_COLS_SALES)
        {
    
            for(i=0;i < a ; i++)
            {
                for(j = 0; j < b; j++)
                {
                    fscanf(fl, "%d", &salesArray[i][j]);
                    printf("%d ",salesArray[i][j]);
    
                }
                printf("\n");
    
            }
    
        }
        else
          printf(" ***Warning: data file is too large *** \n");
    
    fclose(fl);
    return b;
    
    }
    
     void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES])//, double sum[MAX_ROWS_SALES])
    {
        int row=0;
        int col=0;
    
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                total[row][col] =  salesArray[row][col] * PRICE_OF_PRODUCT;
              // sum[row] +=total[row][col];
    
            }
            //printf("\n");
        }
        printf("\n\n");
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                printf("%8.2lf  ", total[row][col]);
    
             // printf("%8.21lf   ", sum[row]);
    
            }
            printf("\n");
        }
        return;
    }
    
    
    double calcHiLowTotal(int size, int numWeeks, double calc[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
    {
    int row=0;
    int col=0;
    
    double comp;
    
    
    
    //prints the total for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            for (row = 0; row < size; row++)
                {
              total_sales[col] += calc[row][col];
                }
    
        }
        printf("\n");
    
    
    
    
    //prints the lowest for each week's sales
     for (col = 0; col <numWeeks; col++)
        {
            low[col] = calc[0][col];
            for (row = 0; row < size; row++)
                {
              if(low[col] > calc[row][col])
                low[col] = calc[row][col];
                }
        }
    
    
    
    
    //prints the highest for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            high[col] = calc[0][col];
            for (row = 0; row < size; row++)
                {
              if(high[col] < calc[row][col])
                high[col] = calc[row][col];
                }
        }
    
    
    //prints company total
     for(col = 0; col < numWeeks; col++)
           {
    
            comp += total_sales[col];
    
    
    
             }
    
    
        return comp;
    
    }
    the output i get is:
    Code:
    *** Salespersons ***
    
    Kelly, Victor
    
    Lam, Gary
    
    Nagasake, David
    
    Nguyen, Jeff
    
    Nguyen, Michael
    
    Sinn, Scott
    
    Smith, Jacob
    
    Son, Thai
    
    Tavares, Maribel
    
    Tran, Diane
    
    Tsukamoto, Andrew
    
    Wang, Mary
    
    Young, Daniel
    
    Wells, Steve
    
    Wong, Justin
    
    Johnson, Mary
    25 20 25 25
    20 22 23 22
    25 26 25 22
    30 28 25 26
    25 30 45 20
    30 25 20 21
    27 25 24 26
    20 23 24 20
    28 26 24 25
    30 10 35 32
    28 29 30 35
    15 16 15 14
    12 15 12 19
    20 24 20 18
    10 15 12 16
    32 30 33 29
    
    
    49648.75  39719.00  49648.75  49648.75
    39719.00  43690.90  45676.85  43690.90
    49648.75  51634.70  49648.75  43690.90
    59578.50  55606.60  49648.75  51634.70
    49648.75  59578.50  89367.75  39719.00
    59578.50  49648.75  39719.00  41704.95
    53620.65  49648.75  47662.80  51634.70
    39719.00  45676.85  47662.80  39719.00
    55606.60  51634.70  47662.80  49648.75
    59578.50  19859.50  69508.25  63550.40
    55606.60  57592.55  59578.50  69508.25
    29789.25  31775.20  29789.25  27803.30
    23831.40  29789.25  23831.40  37733.05
    39719.00  47662.80  39719.00  35747.10
    19859.50  29789.25  23831.40  31775.20
    63550.40  59578.50  65536.35  57592.55
    
    
    Process returned 4 (0x4)   execution time : 0.290 s
    Press any key to continue.
    so the names are from the getNames function, the 4 columns of integers are from the getSales function, and the 4 columns of floats is from the calcSales function, in which I calculate the amount of sales each person made per week.

    the sales.txt is this:
    Code:
    4
    25 20 25 25
    20 22 23 22
    25 26 25 22
    30 28 25 26
    25 30 45 20
    30 25 20 21
    27 25 24 26
    20 23 24 20
    28 26 24 25
    30 10 35 32
    28 29 30 35
    15 16 15 14
    12 15 12 19
    20 24 20 18
    10 15 12 16
    32 30 33 29
    notice how the first number of this file, the number 4, is ignored when I store the .txt file into an array and print the array. Its supposed to be ignored. this number stands for the number of weeks, or columns the txt file has. it's just for our own knowledge.
    Then, the same goes for the names.txt file. it contains names, in the format of: last name, first name

    Code:
    16
    Kelly, Victor       
    Lam, Gary           
    Nagasake, David     
    Nguyen, Jeff        
    Nguyen, Michael     
    Sinn, Scott         
    Smith, Jacob        
    Son, Thai           
    Tavares, Maribel    
    Tran, Diane         
    Tsukamoto, Andrew   
    Wang, Mary          
    Young, Daniel       
    Wells, Steve        
    Wong, Justin        
    Johnson, Mary
    ANY HELP WOULD BE GREATLYYY APPRECIATED!!!!
    Last edited by kal123456; 02-01-2013 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, looks like you have your getXXX functions working now, which is good, but you will have to change them (don't worry, easy stuff) to get the program to print correctly. You need to remove all the print statements from the get functions. I would create a function that prints the sales data like you show above. Something like:
    Code:
    void outputSalesData(int namesArray[][MAX_COLS_NAMES], int numNames, int salesArray[][MAX_COLS_SALES], int numSales)
    That function will print the header row and the table of names/sales data.

    Beyond that, I don't know what your problem is, exactly. Once again, you need to tell us exactly what doesn't work about it. Your table of sales totals looks correct to me. At some point you need to actually print out the other statistics you calculated. The exact format depends on the assignment specifications.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Oh, yes, my assignment requires us to make a function that prints all the data out to the screen, I just thought I would work on that later when I got all the data to print in the separate functions..but I think you're right, I'll write one right now that prints the namesArray, salesArray, and the table of sales totals.
    And i'm sorry I wasnt clrear on that!! :O
    Ok, so the problem is that when I add the printf's after each of the for loops in the calcHighLowTotal, and i make the printf's to print the low, high, total, and comp (comp is the total company sales), the function prints something totaly bogus, junks values or something. so I deleted all the printf's, so basically right now in the code that i gave you, the calcHighLowTotal doesn't print anything, only calculates.Right now, I will include only one printf that should print the lowest for each column of total sales, and you will see that somehow it won't print the right values!!

    here is my updated code, where I added the printf statement in the calcHighLowTotal function, right in the for loops that calculate the lowest for each column:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define PRICE_OF_PRODUCT 1985.95
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    int getSales(int salesArray [][MAX_COLS_SALES], int a);
    void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES]);//, double sum[MAX_ROWS_SALES]);
    double calcHiLowTotal(int size, int numWeeks, double calc[][MAX_COLS_SALES], double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES], double high[MAX_ROWS_SALES]);
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
    
       double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       //double sum[MAX_ROWS_SALES] = {0};
       double calc[][MAX_COLS_SALES] = {0};
       double total_sales[MAX_ROWS_SALES] = {0};
       double low[MAX_ROWS_SALES] = {0};
       double high[MAX_ROWS_SALES] = {0};
    
       int i = 0;
       int j = 0;
       int row;
       int col;
       int numNames=0;
       int numWeeks=0;
      //Statements
    
       printf("*** Salespersons *** \n");
    
    
       numNames=getNames (namesArray);
       numWeeks=getSales (salesArray, numNames);
       calcSales(numNames, numWeeks, salesArray, total);//, sum);
       calcHiLowTotal (numNames, numWeeks, calc, total_sales, low, high);
    
    
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    int a;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
    
    
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
           fscanf(fp, "%d", &a);
        if (a <= MAX_COLS_NAMES)
        {
    
    
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
                {
    
                fscanf(fp, "%c", &namesArray[i][j]);
                printf("%c", namesArray[i][j]);
         }
         printf("\n");
     }
        }
        else
            printf(" ***Warning: data file is too large *** \n");
    
    
    
    
    fclose(fp);
    
    return a;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES], int a)
    {
        //Statements
    FILE *fl;
    int i=0, j=0, c, b;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
        if(fl == NULL)
        {
            printf("Error, can't open file!!!\n");
            exit(101);
        }
    
        fscanf(fl, "%d", &b);
    
        if (b < MAX_COLS_SALES)
        {
    
            for(i=0;i < a ; i++)
            {
                for(j = 0; j < b; j++)
                {
                    fscanf(fl, "%d", &salesArray[i][j]);
                    printf("%d ",salesArray[i][j]);
    
                }
                printf("\n");
    
            }
    
        }
        else
          printf(" ***Warning: data file is too large *** \n");
    
    fclose(fl);
    return b;
    
    }
    
     void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES])//, double sum[MAX_ROWS_SALES])
    {
        int row=0;
        int col=0;
    
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                total[row][col] =  salesArray[row][col] * PRICE_OF_PRODUCT;
              // sum[row] +=total[row][col];
    
            }
            //printf("\n");
        }
        printf("\n\n");
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                printf("%8.2lf  ", total[row][col]);
    
             // printf("%8.21lf   ", sum[row]);
    
            }
            printf("\n");
        }
        return;
    }
    
    
    double calcHiLowTotal(int size, int numWeeks, double calc[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
    {
    int row=0;
    int col=0;
    
    double comp;
    
    
    
    //prints the total for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            for (row = 0; row < size; row++)
                {
              total_sales[col] += calc[row][col];
                }
                //printf("%f", total_sales[col]);
        }
        printf("\n");
    
    
    
    
    //prints the lowest for each week's sales
     for (col = 0; col <numWeeks; col++)
        {
            low[col] = calc[0][col];
            for (row = 0; row < size; row++)
                {
              if(low[col] > calc[row][col])
                low[col] = calc[row][col];
    
                }
                printf("%f", low[col]);
        }
    
    
    
    
    //prints the highest for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            high[col] = calc[0][col];
            for (row = 0; row < size; row++)
                {
              if(high[col] < calc[row][col])
                high[col] = calc[row][col];
                }
        }
    
    
    //prints company total
     for(col = 0; col < numWeeks; col++)
           {
    
            comp += total_sales[col];
    
    
    
             }
    
    
        return comp;
    
    }
    instead of printing the lowest for each column from the total sales table, it prints me 0.00000 as the lowest....

    here's the output i get:
    Code:
    *** Salespersons ***
    
    Kelly, Victor
    
    Lam, Gary
    
    Nagasake, David
    
    Nguyen, Jeff
    
    Nguyen, Michael
    
    Sinn, Scott
    
    Smith, Jacob
    
    Son, Thai
    
    Tavares, Maribel
    
    Tran, Diane
    
    Tsukamoto, Andrew
    
    Wang, Mary
    
    Young, Daniel
    
    Wells, Steve
    
    Wong, Justin
    
    Johnson, Mary
    25 20 25 25
    20 22 23 22
    25 26 25 22
    30 28 25 26
    25 30 45 20
    30 25 20 21
    27 25 24 26
    20 23 24 20
    28 26 24 25
    30 10 35 32
    28 29 30 35
    15 16 15 14
    12 15 12 19
    20 24 20 18
    10 15 12 16
    32 30 33 29
    
    
    49648.75  39719.00  49648.75  49648.75
    39719.00  43690.90  45676.85  43690.90
    49648.75  51634.70  49648.75  43690.90
    59578.50  55606.60  49648.75  51634.70
    49648.75  59578.50  89367.75  39719.00
    59578.50  49648.75  39719.00  41704.95
    53620.65  49648.75  47662.80  51634.70
    39719.00  45676.85  47662.80  39719.00
    55606.60  51634.70  47662.80  49648.75
    59578.50  19859.50  69508.25  63550.40
    55606.60  57592.55  59578.50  69508.25
    29789.25  31775.20  29789.25  27803.30
    23831.40  29789.25  23831.40  37733.05
    39719.00  47662.80  39719.00  35747.10
    19859.50  29789.25  23831.40  31775.20
    63550.40  59578.50  65536.35  57592.55
    
    0.000000  0.000000  0.000000  0.000000
    
    
    Process returned 4 (0x4)   execution time : 0.250 s
    Press any key to continue.
    so yeah, I think the problem is somewhere in my for loops....
    and I will start writing my outputSalesData function meanwhile!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're using the calc parameter to calculate your lows and highs. That is passed in from the calc array in main, which is initialized to all zeros. Should you be passing in the salesArray or total variables instead?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        if (a <= MAX_COLS_NAMES)
        {
     
     
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
    First of all, the number of rows in your array is MAX_ROWS_NAMES

    Second, using <= means you're running off the end(s) of your arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    OMG.....YOU GUYS ARE SERIOUSLY AMAZING!!! it would take me half a day to realize that I was passing in the wrong array!! DUHH of course it would print me 0.0000 cause there are no values in the array!! So i just changed it to the total_sales array because that's the array the low and high have to take their values from so they can compare them, and NOW EVERYTHING WORKS!!!!! THANKS SO MUCH @anduril462

    and @Salem OHH yeah, I didn't even catch that mistake either! Thanks for pointing that out, i fixed it!!
    this is my output now!!
    Code:
    *** Salespersons ***
    
    Kelly, Victor
    
    Lam, Gary
    
    Nagasake, David
    
    Nguyen, Jeff
    
    Nguyen, Michael
    
    Sinn, Scott
    
    Smith, Jacob
    
    Son, Thai
    
    Tavares, Maribel
    
    Tran, Diane
    
    Tsukamoto, Andrew
    
    Wang, Mary
    
    Young, Daniel
    
    Wells, Steve
    
    Wong, Justin
    
    Johnson, Mary
    25 20 25 25
    
    20 22 23 22
    
    25 26 25 22
    
    30 28 25 26
    
    25 30 45 20
    
    30 25 20 21
    
    27 25 24 26
    
    20 23 24 20
    
    28 26 24 25
    
    30 10 35 32
    
    28 29 30 35
    
    15 16 15 14
    
    12 15 12 19
    
    20 24 20 18
    
    10 15 12 16
    
    32 30 33 29
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    49648.75  39719.00  49648.75  49648.75  *----TOTAL SALES---*188665.250000
    
    39719.00  43690.90  45676.85  43690.90  *----TOTAL SALES---*172777.650000
    
    49648.75  51634.70  49648.75  43690.90  *----TOTAL SALES---*194623.100000
    
    59578.50  55606.60  49648.75  51634.70  *----TOTAL SALES---*216468.550000
    
    49648.75  59578.50  89367.75  39719.00  *----TOTAL SALES---*238314.000000
    
    59578.50  49648.75  39719.00  41704.95  *----TOTAL SALES---*190651.200000
    
    53620.65  49648.75  47662.80  51634.70  *----TOTAL SALES---*202566.900000
    
    39719.00  45676.85  47662.80  39719.00  *----TOTAL SALES---*172777.650000
    
    55606.60  51634.70  47662.80  49648.75  *----TOTAL SALES---*204552.850000
    
    59578.50  19859.50  69508.25  63550.40  *----TOTAL SALES---*212496.650000
    
    55606.60  57592.55  59578.50  69508.25  *----TOTAL SALES---*242285.900000
    
    29789.25  31775.20  29789.25  27803.30  *----TOTAL SALES---*119157.000000
    
    23831.40  29789.25  23831.40  37733.05  *----TOTAL SALES---*115185.100000
    
    39719.00  47662.80  39719.00  35747.10  *----TOTAL SALES---*162847.900000
    
    19859.50  29789.25  23831.40  31775.20  *----TOTAL SALES---*105255.350000
    
    63550.40  59578.50  65536.35  57592.55  *----TOTAL SALES---*246257.800000
    
    
    
    ***Person total***
    748703.150000   722885.800000   778492.400000   734801.500000
    
    
      ****Lowest sale for each week ****
    19859.500000  19859.500000  23831.400000  27803.300000
    
       ****Highest sale for each week****
    63550.400000  59578.500000  89367.750000  69508.250000
    
    Process returned 2683524 (0x28F284)   execution time : 0.313 s
    Press any key to continue.
    HOWEVER GUYS, I"M NOT DONE ASKING QUESTIONS YETTT!!! hahaha

    this is my current code:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define PRICE_OF_PRODUCT 1985.95
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    int getSales(int salesArray [][MAX_COLS_SALES], int a);
    void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES]);
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES], double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES], double high[MAX_ROWS_SALES]);
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
    
       double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       double sum[MAX_ROWS_SALES] = {0};
       //double calc[][MAX_COLS_SALES] = {0};
       double total_sales[MAX_ROWS_SALES] = {0};
       double low[MAX_ROWS_SALES] = {0};
       double high[MAX_ROWS_SALES] = {0};
    
       int i = 0;
       int j = 0;
       int row;
       int col;
       int numNames=0;
       int numWeeks=0;
      //Statements
    
       printf("*** Salespersons *** \n");
    
    
       numNames=getNames (namesArray);
       numWeeks=getSales (salesArray, numNames);
       calcSales(numNames, numWeeks, salesArray, total, sum);
    
       printf("\n\n***Person total***  \n");
       calcHiLowTotal (numNames, numWeeks, total, total_sales, low, high);
    
    
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    int a;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
    
    
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
           fscanf(fp, "%d", &a);
        if (a <= MAX_ROWS_NAMES)
        {
    
    
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
                {
    
                fscanf(fp, "%c", &namesArray[i][j]);
                printf("%c", namesArray[i][j]);
         }
         printf("\n");
     }
        }
        else
            printf(" ***Warning: data file is too large *** \n");
    
    
    
    
    fclose(fp);
    
    return a;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES], int a)
    {
        //Statements
    FILE *fl;
    int i=0, j=0, c, b;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
        if(fl == NULL)
        {
            printf("Error, can't open file!!!\n");
            exit(101);
        }
    
        fscanf(fl, "%d", &b);
    
        if (b < MAX_COLS_SALES)
        {
    
            for(i=0;i < a ; i++)
            {
                for(j = 0; j < b; j++)
                {
                    fscanf(fl, "%d", &salesArray[i][j]);
                    printf("%d ",salesArray[i][j]);
    
                }
                printf("\n\n");
    
            }
    
        }
        else
          printf(" ***Warning: data file is too large *** \n");
    
    fclose(fl);
    return b;
    
    }
    
     void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES])
    {
        int row=0;
        int col=0;
    
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                total[row][col] =  salesArray[row][col] * PRICE_OF_PRODUCT;
              sum[row] +=total[row][col];
    
            }
            printf("\n");
        }
        printf("\n\n");
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                printf("%8.2lf  ", total[row][col]);
    
    
    
            }
            printf("*----TOTAL SALES---*");
            printf("%f   ", sum[row]);
            printf("\n\n");
        }
        return;
    }
    
    
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
    {
    int row=0;
    int col=0;
    
    double comp;
    
    
    
    //prints the total for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            for (row = 0; row < size; row++)
                {
              total_sales[col] += total[row][col];
                }
                printf("%f   ", total_sales[col]);
        }
        printf("\n\n\n  ");
    
    
    
    printf("****Lowest sale for each week ****\n");
    //prints the lowest for each week's sales
     for (col = 0; col <numWeeks; col++)
        {
            low[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(low[col] > total[row][col])
                low[col] = total[row][col];
    
                }
    
                printf("%f  ", low[col]);
    
        }
    
    
    printf("    \n\n   ");
    printf("****Highest sale for each week**** \n");
    //prints the highest for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            high[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(high[col] < total[row][col])
                high[col] = total[row][col];
                }
    
                printf("%f  ", high[col]);
        }
    
    
    //prints company total
     for(col = 0; col < numWeeks; col++)
           {
    
            comp += total_sales[col];
    
    
    
             }
             printf("\n");
            // printf("%d", comp);
    
    
        return comp;
    
    }
    
    
    //void outputSalesData(int namesArray[][MAX_COLS_NAMES], int numNames, int numSales, int salesArray[][MAX_COLS_SALES])
    I think I'll need help printing everything in the print to screen function!! Like, the way the table is gonna look, the orientation!
    And of course, I'll need a little help with the print to output file function too. I'll get back to you guys very soon, hope you don't mind!!

  7. #7
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Ok guys, I'm back, I knew it -_____- soooo, just now I realized that when I calculated the total amount of sales each person had for all 4 weeks, the amount should have been stored in a one dimensional array....and I stored it in a two dimensional array!! Now i'm trying to print the total amount of sales for each person, and the values come out to 0.000
    is it possible for me to go back to my program and change the code so the sale totals for each person are stored in a 1D array? or is that too much fuss, and there's a way to tweek something in my function? The problem is in my last function, outputSalesDataToFile, in the last for loop, where I'm trying to print the two dimensional array called "total".
    Could you please review that part of code and then my calcSales function (where i'm storing the total sales for each person in the 2D array "total") and tell me if I can change the 2D to a 1D array, and any other suggestions you might have?!

    THANKS SO MUCH!!
    this is my code:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define PRICE_OF_PRODUCT 1985.95
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    
    int getSales(int salesArray [][MAX_COLS_SALES], int a);
    
    void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES]);
    
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES],
                          double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES],
                           double high[MAX_ROWS_SALES]);
    
    void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES]);
    
    void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
                                double low[],double high[], double sum[], int numNames, int numWeeks);
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
    
       double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       double sum[MAX_ROWS_SALES] = {0};
       double total_sales[MAX_ROWS_SALES] = {0};
       double low[MAX_ROWS_SALES] = {0};
       double high[MAX_ROWS_SALES] = {0};
    
       int i = 0;
       int j = 0;
       int row;
       int col;
       int numNames=0;
       int numWeeks=0;
      //Statements
    
      // printf("*** Salespersons *** \n");
    
    
       numNames=getNames (namesArray);
       numWeeks=getSales (salesArray, numNames);
       calcSales(numNames, numWeeks, salesArray, total, sum);
     //  printf("\n\n***Person total***  \n");
       calcHiLowTotal (numNames, numWeeks, total, total_sales, low, high);
       outputSalesDataToScreen (namesArray, numNames, numWeeks, salesArray);
       outputSalesDataToFile(namesArray, salesArray, total, total_sales, low, high, sum, numNames, numWeeks);
    
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    int a;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
    
    
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
           fscanf(fp, "%d", &a);
        if (a <= MAX_ROWS_NAMES)
        {
    
    
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
                {
    
                fscanf(fp, "%c", &namesArray[i][j]);
               // printf("%c", namesArray[i][j]);
         }
        // printf("\n");
     }
        }
        else
            printf(" ***Warning: data file is too large *** \n");
    
    
    
    
    fclose(fp);
    
    return a;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES], int a)
    {
        //Statements
    FILE *fl;
    int i=0, j=0, c, b;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
        if(fl == NULL)
        {
            printf("Error, can't open file!!!\n");
            exit(101);
        }
    
        fscanf(fl, "%d", &b);
    
        if (b < MAX_COLS_SALES)
        {
    
            for(i=0;i < a ; i++)
            {
                for(j = 0; j < b; j++)
                {
                    fscanf(fl, "%d", &salesArray[i][j]);
                   // printf("%d ",salesArray[i][j]);
    
                }
               // printf("\n\n");
    
            }
    
        }
        else
          printf(" ***Warning: data file is too large *** \n");
    
    fclose(fl);
    return b;
    
    }
    
     void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES])
    {
        int row=0;
        int col=0;
    
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                total[row][col] =  salesArray[row][col] * PRICE_OF_PRODUCT;
              sum[row] +=total[row][col];
    
            }
          //  printf("\n");
        }
       // printf("\n\n");
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
               // printf("%8.2lf  ", total[row][col]);
    
    
    
            }
    
           // printf("*----TOTAL SALES---*");
           // printf("%f   ", sum[row]);
           // printf("\n\n");
        }
        return;
    }
    
    
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
    {
    int row=0;
    int col=0;
    
    double comp;
    
    
    
    //prints the total for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            for (row = 0; row < size; row++)
                {
              total_sales[col] += total[row][col];
                }
               // printf("%f   ", total_sales[col]);
        }
       // printf("\n\n\n  ");
    
    
    
    //printf("****Lowest sale for each week ****\n");
    //prints the lowest for each week's sales
     for (col = 0; col <numWeeks; col++)
        {
            low[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(low[col] > total[row][col])
                low[col] = total[row][col];
    
                }
    
                //printf("%f  ", low[col]);
    
        }
    
    
    //printf("    \n\n   ");
    //printf("****Highest sale for each week**** \n");
    //prints the highest for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            high[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(high[col] < total[row][col])
                high[col] = total[row][col];
                }
    
                //printf("%f  ", high[col]);
        }
    
    
    //prints company total
     for(col = 0; col < numWeeks; col++)
           {
    
            comp += total_sales[col];
    
    
    
             }
            // printf("\n");
            // printf("%d", comp);
    
    
        return comp;
    
    }
    
    
    void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES])
    {
        //Local Declarations
        int i, j, k;
    
        //Statements
        printf(" \n                     Homework 2: Two Dimensional Arrays\n\n");
        printf( "                           *** Sales Table *** \n\n\n");
        printf("==================    ========         ========        ========       ========\n");
        printf("  Sales Person         Week 1           Week 2          Week 3          Week 4\n");
        printf("==================    ========         ========        ========       ========\n");
    
        for (i=0; i<numNames; i++)
        {
            for (j=0; j<MAX_COLS_NAMES; j++)
            {
                printf("%c", namesArray[i][j]);
            }
            printf("\n         ");
            for (k=0; k<numWeeks; k++)
            {
                printf("               %2d", salesArray[i][k]);
            }
    
        }
        printf("   \n\n\n                    *** The output is in SALESRES.TXT *** \n\n");
        return;
    
    
    
    }
    
    void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
                                double low[],double high[], double sum[], int numNames, int numWeeks)
        {
    
            //Local Declarations
            FILE* salesOutput;
            int i, j, k, l;
    
            //Statements
    
            if(!(salesOutput = fopen("SALESRES.TXT", "w")))
                {
            printf("can not open the 'SALESERS.TXT' file.\n");
            exit(101);
                }
    
    
            fprintf(salesOutput, "===========================\n");
            fprintf(salesOutput, " ABC Manufacturing Company\n");
            fprintf(salesOutput, "===========================\n\n\n");
            fprintf(salesOutput, "*** Dollar Values Table *** \n\n");
            fprintf(salesOutput, "==================== ========== ========== ========== ==========  =============\n");
            fprintf(salesOutput, "   Sales Person        Week 1     Week 2     Week 3     Week 4     Total/Person\n");
            fprintf(salesOutput, "====================  ========== ========== ========== ==========  =============\n");
    
            for(i = 0; i < numNames; i++)
        {
                for(j =0; j < MAX_COLS_NAMES; j++)
                    {
                        fprintf(salesOutput, "%c", namesArray[i][j]);
                    }
                for(k =0; k < numWeeks; k++)
                    {
                        fprintf(salesOutput, "   %8.2lf", salesArray[i][k] * PRICE_OF_PRODUCT);
    
                    }
                fprintf(salesOutput, "    %8.2lf", total[i][k] * PRICE_OF_PRODUCT);
    
    
        }
         fclose(salesOutput);
                return;
        }
    and this is the output file i'm getting, with the weird zeros...
    Code:
    ===========================
     ABC Manufacturing Company
    ===========================
    
    
    *** Dollar Values Table *** 
    
    ==================== ========== ========== ========== ==========  =============
       Sales Person        Week 1     Week 2     Week 3     Week 4     Total/Person
    ====================  ========== ========== ========== ==========  =============
    
    Kelly, Victor         49648.75   39719.00   49648.75   49648.75        0.00
    Lam, Gary             39719.00   43690.90   45676.85   43690.90        0.00
    Nagasake, David       49648.75   51634.70   49648.75   43690.90        0.00
    Nguyen, Jeff          59578.50   55606.60   49648.75   51634.70        0.00
    Nguyen, Michael       49648.75   59578.50   89367.75   39719.00        0.00
    Sinn, Scott           59578.50   49648.75   39719.00   41704.95        0.00
    Smith, Jacob          53620.65   49648.75   47662.80   51634.70        0.00
    Son, Thai             39719.00   45676.85   47662.80   39719.00        0.00
    Tavares, Maribel      55606.60   51634.70   47662.80   49648.75        0.00
    Tran, Diane           59578.50   19859.50   69508.25   63550.40        0.00
    Tsukamoto, Andrew     55606.60   57592.55   59578.50   69508.25        0.00
    Wang, Mary            29789.25   31775.20   29789.25   27803.30        0.00
    Young, Daniel         23831.40   29789.25   23831.40   37733.05        0.00
    Wells, Steve          39719.00   47662.80   39719.00   35747.10        0.00
    Wong, Justin          19859.50   29789.25   23831.40   31775.20        0.00
    Johnson, Mary         63550.40   59578.50   65536.35   57592.55        0.00

  8. #8
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    NEVERMIND ACTUALLY, I FOUND MY ERROR, SO NOW I CAN PRINT THE TOTAL/PERSON.
    the only LASTT problem is that I cant print the Total Sales for each week!! I print it fine in my calcSales function, the correct values come out there (there are 4 values that come out). but when I try to print it in my last output to file function, it just prints 0.0000!!! WHY?! IS MY FOR LOOP WRONG?! I COPIED THE EXACT SAME FOR LOOP INTO MY LAST FUNCTION AND IT STILL WONT WORK!!!

    my code:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define PRICE_OF_PRODUCT 1985.95
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    
    int getSales(int salesArray [][MAX_COLS_SALES], int a);
    
    void calcSales(int size, int numWeeks, int salesArrays[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES]);
    
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES],
                          double total_sales[MAX_ROWS_SALES], double low[MAX_ROWS_SALES],
                           double high[MAX_ROWS_SALES]);
    
    void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES]);
    
    void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
                                double low[],double high[], double sum[], int numNames, int numWeeks);
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
    
       double total[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       double sum[MAX_ROWS_SALES] = {0};
       double total_sales[MAX_ROWS_SALES] = {0};
       double low[MAX_ROWS_SALES] = {0};
       double high[MAX_ROWS_SALES] = {0};
    
       int i = 0;
       int j = 0;
       int row;
       int col;
       int numNames=0;
       int numWeeks=0;
      //Statements
    
      // printf("*** Salespersons *** \n");
    
    
       numNames=getNames (namesArray);
       numWeeks=getSales (salesArray, numNames);
       calcSales(numNames, numWeeks, salesArray, total, sum);
     //  printf("\n\n***Person total***  \n");
       calcHiLowTotal (numNames, numWeeks, total, total_sales, low, high);
       outputSalesDataToScreen (namesArray, numNames, numWeeks, salesArray);
       outputSalesDataToFile(namesArray, salesArray, total, total_sales, low, high, sum, numNames, numWeeks);
    
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    int a;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
    
    
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
           fscanf(fp, "%d", &a);
        if (a <= MAX_ROWS_NAMES)
        {
    
    
            for(i=0;i<a;i++)
            {
                for(j = 0; j <= MAX_COLS_NAMES; j++)
                {
    
                fscanf(fp, "%c", &namesArray[i][j]);
               // printf("%c", namesArray[i][j]);
         }
        // printf("\n");
     }
        }
        else
            printf(" ***Warning: data file is too large *** \n");
    
    
    
    
    fclose(fp);
    
    return a;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES], int a)
    {
        //Statements
    FILE *fl;
    int i=0, j=0, c, b;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
        if(fl == NULL)
        {
            printf("Error, can't open file!!!\n");
            exit(101);
        }
    
        fscanf(fl, "%d", &b);
    
        if (b < MAX_COLS_SALES)
        {
    
            for(i=0;i < a ; i++)
            {
                for(j = 0; j < b; j++)
                {
                    fscanf(fl, "%d", &salesArray[i][j]);
                   // printf("%d ",salesArray[i][j]);
    
                }
               // printf("\n\n");
    
            }
    
        }
        else
          printf(" ***Warning: data file is too large *** \n");
    
    fclose(fl);
    return b;
    
    }
    
     void calcSales(int size, int numWeeks, int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double sum[MAX_ROWS_SALES])
    {
        int row=0;
        int col=0;
    
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
                total[row][col] =  salesArray[row][col] * PRICE_OF_PRODUCT;
              sum[row] +=total[row][col];
    
            }
          //  printf("\n");
        }
       // printf("\n\n");
    
    
        for (row=0;row <size; row++)
        {
            for (col=0; col<numWeeks; col++)
            {
               // printf("%8.2lf  ", total[row][col]);
    
    
    
            }
    
           // printf("*----TOTAL SALES---*");
           // printf("%f   ", sum[row]);
           // printf("\n\n");
        }
        return;
    }
    
    
    double calcHiLowTotal(int size, int numWeeks, double total[][MAX_COLS_SALES], double total_sales[], double low[], double high[])
    {
    int row=0;
    int col=0;
    
    double comp;
    
    
    
    //prints the total for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            for (row = 0; row < size; row++)
                {
              total_sales[col] += total[row][col];
                }
               // printf("%f   ", total_sales[col]);
        }
       // printf("\n\n\n  ");
    
    
    
    //printf("****Lowest sale for each week ****\n");
    //prints the lowest for each week's sales
     for (col = 0; col <numWeeks; col++)
        {
            low[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(low[col] > total[row][col])
                low[col] = total[row][col];
    
                }
    
                //printf("%f  ", low[col]);
    
        }
    
    
    //printf("    \n\n   ");
    //printf("****Highest sale for each week**** \n");
    //prints the highest for each week's sales
     for (col = 0; col < numWeeks; col++)
        {
            high[col] = total[0][col];
            for (row = 0; row < size; row++)
                {
              if(high[col] < total[row][col])
                high[col] = total[row][col];
                }
    
                //printf("%f  ", high[col]);
        }
    
    
    //prints company total
     for(col = 0; col < numWeeks; col++)
           {
    
            comp += total_sales[col];
    
    
    
             }
            // printf("\n");
            // printf("%d", comp);
    
    
        return comp;
    
    }
    
    
    void outputSalesDataToScreen(char namesArray[][MAX_COLS_NAMES], int numNames, int numWeeks, int salesArray[][MAX_COLS_SALES])
    {
        //Local Declarations
        int i, j, k;
    
        //Statements
        printf(" \n                     Homework 2: Two Dimensional Arrays\n\n");
        printf( "                           *** Sales Table *** \n\n\n");
        printf("==================    ========         ========        ========       ========\n");
        printf("  Sales Person         Week 1           Week 2          Week 3          Week 4\n");
        printf("==================    ========         ========        ========       ========\n");
    
        for (i=0; i<numNames; i++)
        {
            for (j=0; j<MAX_COLS_NAMES; j++)
            {
                printf("%c", namesArray[i][j]);
            }
            printf("\n         ");
            for (k=0; k<numWeeks; k++)
            {
                printf("               %2d", salesArray[i][k]);
            }
    
        }
        printf("   \n\n\n                    *** The output is in SALESRES.TXT *** \n\n");
        return;
    
    
    
    }
    
    void outputSalesDataToFile(char namesArray[][MAX_COLS_NAMES], int salesArray[][MAX_COLS_SALES], double total[][MAX_COLS_SALES], double total_sales[],
                                double low[],double high[], double sum[], int numNames, int numWeeks)
        {
    
            //Local Declarations
            FILE* salesOutput;
            int i, j, k, l;
            double comp;
    
            //Statements
    
            if(!(salesOutput = fopen("SALESRES.TXT", "w")))
                {
            printf("can not open the 'SALESERS.TXT' file.\n");
            exit(101);
                }
    
    
            fprintf(salesOutput, "===========================\n");
            fprintf(salesOutput, " ABC Manufacturing Company\n");
            fprintf(salesOutput, "===========================\n\n\n");
            fprintf(salesOutput, "*** Dollar Values Table *** \n\n");
            fprintf(salesOutput, "==================== ========== ========== ========== ==========  =============\n");
            fprintf(salesOutput, "   Sales Person        Week 1     Week 2     Week 3     Week 4     Total/Person\n");
            fprintf(salesOutput, "====================  ========== ========== ========== ==========  =============\n");
    
            for(i = 0; i < numNames; i++)
        {
                for(j =0; j < MAX_COLS_NAMES; j++)
                    {
                        fprintf(salesOutput, "%c", namesArray[i][j]);
                    }
                for(k =0; k < numWeeks; k++)
                    {
                        fprintf(salesOutput, "   %8.2lf", salesArray[i][k] * PRICE_OF_PRODUCT);
    
                    }
                fprintf(salesOutput, "    %8.2lf", sum[i]);
    
    
        }
    
        fprintf(salesOutput, "\n==================== ========== ========== ========== ==========");
        fprintf(salesOutput, "\n     Total / Week: ");
    
    
        for (i = 0; i < numWeeks; i++)
        {
            comp += total_sales[i];
            for (j = 0; j < numNames; j++)
                {
              total_sales[i] += total[i][j];
    
                }
    
               printf("%f   ", total_sales[i]);
        }
    
    
    
             fprintf(salesOutput, "  %9.2lf", total_sales[i]);
        fprintf(salesOutput, "\n             LOW : ");
        for(l =0; l < numWeeks; l++)
            fprintf(salesOutput, "  %9.2lf", low[l]);
        fprintf(salesOutput, "\n             HIGH: ");
        for(l =0; l < numWeeks; l++)
            fprintf(salesOutput, "  %9.2lf", high[l]);
    
    
        fprintf(salesOutput,  "\n\n *** Total Sales for company:   ");
        fprintf(salesOutput, "%10.2lf ***\n\n", comp);
        fprintf(salesOutput, "=============\n");
        fprintf(salesOutput, "End of Report\n");
        fprintf(salesOutput, "=============\n");
    
    
    
         fclose(salesOutput);
                return;
        }
    the output i get
    Code:
    ===========================
     ABC Manufacturing Company
    ===========================
    
    
    *** Dollar Values Table *** 
    
    ==================== ========== ========== ========== ==========  =============
       Sales Person        Week 1     Week 2     Week 3     Week 4     Total/Person
    ====================  ========== ========== ========== ==========  =============
    
    Kelly, Victor         49648.75   39719.00   49648.75   49648.75    188665.25
    Lam, Gary             39719.00   43690.90   45676.85   43690.90    172777.65
    Nagasake, David       49648.75   51634.70   49648.75   43690.90    194623.10
    Nguyen, Jeff          59578.50   55606.60   49648.75   51634.70    216468.55
    Nguyen, Michael       49648.75   59578.50   89367.75   39719.00    238314.00
    Sinn, Scott           59578.50   49648.75   39719.00   41704.95    190651.20
    Smith, Jacob          53620.65   49648.75   47662.80   51634.70    202566.90
    Son, Thai             39719.00   45676.85   47662.80   39719.00    172777.65
    Tavares, Maribel      55606.60   51634.70   47662.80   49648.75    204552.85
    Tran, Diane           59578.50   19859.50   69508.25   63550.40    212496.65
    Tsukamoto, Andrew     55606.60   57592.55   59578.50   69508.25    242285.90
    Wang, Mary            29789.25   31775.20   29789.25   27803.30    119157.00
    Young, Daniel         23831.40   29789.25   23831.40   37733.05    115185.10
    Wells, Steve          39719.00   47662.80   39719.00   35747.10    162847.90
    Wong, Justin          19859.50   29789.25   23831.40   31775.20    105255.35
    Johnson, Mary         63550.40   59578.50   65536.35   57592.55    246257.80
    ==================== ========== ========== ========== ==========
         Total / Week:        0.00
                 LOW :    19859.50   19859.50   23831.40   27803.30
                 HIGH:    63550.40   59578.50   89367.75   69508.25
    
     *** Total Sales for company:   2984882.85 ***
    
    =============
    End of Report
    =============
    PLEASE HELP THIS IS DUE TONIGHT!!!!!!

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    With nearly 400 lines of code, you should really have better, consistent indentation if you want more people to take the time to help you.

    Lines 338 - 348:

    Code:
    for (i = 0; i < numWeeks; i++)
    {
        comp += total_sales[i];
        for (j = 0; j < numNames; j++)
        {
            total_sales[i] += total[i][j];
        }
        printf("%f   ", total_sales[i]);
    }
    Where are you printing the line in red? Because it's not to your output file.

    Also, please be careful - "comp" is not initialized in this function, and its first use adds a value to itself, resulting in a nonsense value.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    WHY?! IS MY FOR LOOP WRONG?! I COPIED THE EXACT SAME FOR LOOP INTO MY LAST FUNCTION AND IT STILL WONT WORK!!!


    Code:
    tolower(kal123456);
    Be wary of copy/pasting! It can make a lot of things go faster, but can also trip you up for a long time with subtle bugs.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Hey!! I JUST FIXED MY PROBLEMS AND I"M DONE WITH THE HOMEWORK!! Matticus you were right, I didnt print the total_sales[i] to my output file!! and I fixed the part about comp too, so now everything works! and I'm really sorry about my indentation -____- I'm trying to improve on it, this is my first computer programming course haha! Thanks all again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2Dimensional arrays..read from txt file PROBLEM! :O
    By kal123456 in forum C Programming
    Replies: 43
    Last Post: 01-31-2013, 04:36 PM
  2. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  3. adding up array column total
    By dmckay in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:14 PM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM

Tags for this Thread