0

So im doing this homework for my programming class, but the data output doesnt match with the theoretical data output that it shoud be printing and it gives me a big number.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void menu(int *);
void CalculoVentas(float ventas[50][7], float ventasXproductos[50], int);
void PromedioDiario(float promedioXdia[7], float ventas[50][7], int);

int main()
{
    char ciclos;
    int sel, cont = 0, i = 0;
    float ventas[50][7], ventasXproductos[50], promedioXdia[7];
    bool lectura = false;

    while(sel != 5)
        {
            menu(&sel);

            system("cls");

            switch(sel)
            {
                case 1:
                    printf("Hay Productos?: S/N ");
                    fflush(stdin);
                    scanf("%c", &ciclos);
                    while(ciclos == 's' || ciclos == 'S' && cont < 50)
                        {
                            for(i=0;i<7;i++)
                                {
                                    do
                                    {
                                        printf("Teclea los ingresos generados por el producto %d el dia %d: ", cont+1, i+1);
                                        scanf("%f", &ventas[cont][i]);
                                    }while(ventas[cont][i] < 0);
                                    printf("\n\n");
                                }

                            cont++;

                            system("cls");

                            printf("Hay Productos?: S/N ");
                            fflush(stdin);
                            scanf("%c", &ciclos);
                            lectura = true;
                        }

                break;

                case 2:
                    if(lectura == false)
                        printf("Primero ingresa los datos del producto!!!\n");
                    else
                        CalculoVentas(ventas, ventasXproductos, cont);
                break;

                case 3:
                    if(lectura == false)
                        printf("Primero ingresa los datos del producto!!!\n");
                    else
                        PromedioDiario(promedioXdia, ventas, cont);
                break;

                case 4:
                    if(lectura == false)
                        printf("Primero ingresa los datos del producto!!!\n");
                    else
                        printf("%35s\n", "Total de ventas");

                        for(i=0;i<cont;i++)
                            printf("%d %.2f\n", i+1, ventasXproductos[i]);

                        printf("\n\n\n\n");

                        printf("%35s\n", "Promedio de ventas por dia");

                        for(i=0;i<7;i++)
                            printf("Dia %d: %.2f\n", i+1, promedioXdia[i]);

                        printf("\n\n\n\n");

                break;

            }

        }
}

void menu(int *seleccion)
{
    printf("%20s\n%s\n%s\n%s\n%s\n%s\n", "Menu de opciones", "1.-lectura de datos", "2.-calculo de ventas por producto", "3.-promedio de ventas de cada dia", "4.-imprimir resultados", "5.-salir");
        do{
            printf("Seleccione una opcion: ");
            scanf("%d", &*seleccion);
        }while(*seleccion <= 0 || *seleccion > 5);
}

void CalculoVentas(float ventas[50][7], float ventasXproducto[50], int cont)
{
    //realizar el calculo de ventas por productos, nada más es la suma de las ventas de todos los dias por producto
    int i,j;
    for (i = 0 ; i<cont ; i++)
        for (j = 0 ; j < 7 ; j++)
            ventasXproducto[i] = ventasXproducto[i] + ventas[i][j];
}

void PromedioDiario(float promedioXdia[7], float ventas[50][7], int cont)
{
    int i,j;
    for(i=0;i<7;i++)
        {
            for(j=0;j<cont; j++)
                promedioXdia[i] = promedioXdia[i] + ventas[j][i];
            promedioXdia[i] = promedioXdia[i]/cont;
        }
}

the output is something like this, I try to type simple data so i can know easly if its wrong

                    Total de ventas
1 70.00
2 5103881324019006800000000000000000.00
3 210.00
4 280.00




         Promedio de ventas por dia
Dia 1: 25.00
Dia 2: 25.00
Dia 3: 25.00
Dia 4: 1291386862541487300000000000000000.00
Dia 5: 25.00
Dia 6: -1.#R
Dia 7: 25.00

when i was trying to get help someone told me that i might be not initializing correctly the variables but i couldnt find any issue with that

  • 1
    What is the value of `int sel` when you test `while(sel != 5)` on the first iteration? (it's uninitialized) That invokes *Undefined Behavior* in your code -- All bets are off at that point. Fix that first. – David C. Rankin Oct 24 '22 at 05:01

1 Answers1

1

When the program begins, sel is uninitialized and contains a garbage value. This garbage value is used in the while condition on its first iteration

while(sel != 5)

and as such invokes Undefined Behaviour.

You must restructure your loop to not read this uninitialized value, or simply initialize sel (to something other than 5).

Similarly, the contents of ventas, ventasXproductos, and promedioXdia are all uninitialized as well.

This means statements such as

ventasXproducto[i] = ventasXproducto[i] + ventas[i][j];
/* ... and ... */
promedioXdia[i] = promedioXdia[i]/cont;

will be operating with garbage values to start.

You can fix this by initializing your arrays:

float ventas[50][7] = { 0 }, ventasXproductos[50] = { 0 }, promedioXdia[7] = { 0 };

You should not ignore the return value of scanf. You should always check that its return value is the expected number of successful conversions, otherwise you will find yourself operating on incomplete data.

/* An example */
if (2 != scanf("%d%d", &num1, &num2)) {
    /* handle failure */
}

(Better yet: avoid using scanf, and use fgets and sscanf to read and parse lines of input.)


You should clarify this expression by adding more parenthesis, otherwise you will run into issues with operator precedence:

while ((ciclos == 's' || ciclos == 'S') && cont < 50)

case 4 of the switch has misleading indentation. Only the first statement with the call to printf is contained within the else block. It is read as:

if(lectura == false)
    printf("Primero ingresa los datos del producto!!!\n");
else
    printf("%35s\n", "Total de ventas");

for(i=0;i<cont;i++)
    printf("%d %.2f\n", i+1, ventasXproductos[i]);
/* ... */

Your lectura flag will not protect you from operating on incomplete data if this is selected. Enclose the code with curly braces:

case 4:
    if(lectura == false) {
        printf("Primero ingresa los datos del producto!!!\n");
    } else {
        printf("%35s\n", "Total de ventas");

        for(i=0;i<cont;i++)
            printf("%d %.2f\n", i+1, ventasXproductos[i]);

        /* ... */
    }

    break;

Note that in &*seleccion, & and * balance each other out. This resolves to the same value as just writing seleccion would.


Note that fflush(stdin); is also (technically) Undefined Behaviour, and should not be relied upon.

Oka
  • 23,367
  • 6
  • 42
  • 53