Renato96 / Proyecto-Funciones-y-Arreglos

Una escuela de primaria se le solicita a usted realizar un programa para registrar los estudiantes y sus notas para un salón de clases de 10 estudiantes. El programa debe registrar el nombre de los estudiantes, promedio de los parciales, el promedio de las asignaciones, el examen final.
0 stars 0 forks source link

Proyecto #1

Open Renato96 opened 8 years ago

Renato96 commented 8 years ago

Una escuela de primaria se le solicita a usted realizar un programa para registrar los estudiantes y sus notas para un salón de clases de 10 estudiantes. El programa debe registrar el nombre de los estudiantes, promedio de los parciales, el promedio de las asignaciones, el examen final.

Calculo del Promedio = (Parciales * 30%) + (Asig * 30%) + (EFinal * 40%) Una vez tenga el registro de todos los datos el programa debe ser capaz de realizar las siguientes opciones

  1. Calcular el promedio final y asignar la literal para cada estudiante. Seguidamente debe escribir los resultados.
  2. Indicar cuál es el promedio más alto y escribir los datos del estudiante al que corresponde este promedio.
  3. Indicar cuál es el promedio más bajo y escribir los datos del estudiante al que corresponde este promedio.

Proyecto.txt

mishelashala commented 8 years ago

El código es "correcto", pero.. aún no lo he probado.

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_STUDENTS 10

/*!
 * C utility to get string from user
 * By the way, I wrote it when I was 19-yo.
 * If there is some bug report to github.com/mishelashala/cval
 *
 * @author: Michell Ayala <starships@outlook.com>
 * @param void
 * @return char*
 */

char *getString(void) {
    char *buffer = NULL;
    unsigned int capacity = 0;
    unsigned int size = 0;

    // character read or EOF
    int c;

    while ((c = fgetc(stdin)) != '\n' && c != EOF) {
        if (size + 1 > capacity) {

            if (capacity == 0) {
                capacity = 32;
            } else if (capacity <= (UINT_MAX / 2)) {
                capacity *= 2;
            } else {
                free(buffer);
                return NULL;
            }

            // extend buffer's capacity
            char *temp = realloc(buffer, capacity * sizeof(char));

            if (temp == NULL) {
                free(buffer);
                return NULL;
            }

            buffer = temp;
        }

        buffer[size++] = c;
    }

    if (size == 0 && c == EOF)
        return NULL;

    char *minimal = malloc((size + 1) * sizeof(char));
    strncpy(minimal, buffer, size);
    free(buffer);

    minimal[size] = '\0';

    return minimal;
}

/*!
 * C utility to get float number from user
 * By the way, I wrote it when I was 19-yo.
 * If there is some bug report to github.com/mishelashala/cval
 *
 * @author: Michell Ayala <starships@outlook.com>
 * @param void
 * @return float
 */

float getFloat(void) {
    float num;
    char badc;
    char *input;

    while (true) {
        input = getString();

        if (input == NULL)
            return FLT_MAX;

        if (sscanf(input, " %f %c", &num, &badc) == 1) {
            free(input);
            return num;
        } else {
            free(input);
            printf("Error. Enter a floating number: ");
        }
    }
}

/*!
 * C utility to get integer number from user
 * By the way, I wrote it when I was 19-yo.
 * If there is some bug report to github.com/mishelashala/cval
 *
 * @author: Michell Ayala <starships@outlook.com>
 * @param void
 * @return Integer
 */

int getInteger(void) {
    int num;
    char badc;
    char *input;

    while (true) {
      input = getString();

      if (input == NULL)
          return INT_MAX;

      if (sscanf(input, " %d %c", &num, &badc) == 1) {
            free(input);
            return num;
      } else {
            free(input);
            printf("Error. Enter an integer: ");
      }
    }
}

/*!
 * It calculates the average notes' students
 * @param [Float]
 * @param [Integer]
 * @param [Float]
 */

void calculate_average (
  float partials[NUMBER_OF_STUDENTS],
  int asignatures[NUMBER_OF_STUDENTS],
  float finals[NUMBER_OF_STUDENTS]
) {
  int student_index = 0;
  float total_average = 0.0f;

  while (student_index < NUMBER_OF_STUDENTS) {
    total_average =
      (partials[student_index] * 0.3) +
      (asignatures[student_index] * 0.3) +
      (finals[student_index] * 0.4);

    if (total_average <= 10 && total_average > 9) {
      printf("[%d] Nota: A, (%.2f)\n", student_index, total_average);
    } else if (total_average <= 9.0 && total_average > 8) {
      printf("[%d] Nota: B (%.2f)\n", student_index, total_average);
    } else if (total_average <= 8.0 && total_average > 7) {
      printf("[%d] Nota: C (%.2f)\n", student_index, total_average);
    } else if (total_average <= 7.0 && total_average > 6) {
      printf("[%d] Nota: D (%.2f)\n", student_index, total_average);
    } else if (total_average <= 6.0) {
      printf("[%d] Nota: F (%.2f)\n", student_index, total_average);
    }
  }

}

/*!
 * Calculates the highest average among all students
 * @param [Float] - The list of students' notes
 * @return void
 */

void calculate_highest_partial (float partials[NUMBER_OF_STUDENTS]) {
  int index = 0;
  int highest = partials[index];

  while (index < NUMBER_OF_STUDENTS) {
    if (highest < partials[index]) {
        highest = partials[index];
    }

    index++;
  }

  printf("\nEl promedio mas alto es: %d", highest);
}

/*!
 * Calculates the lowest average among all students
 * @param [Float] - The list of students' notes
 * @return void
 */

void calculate_lowest_partial (float partials[NUMBER_OF_STUDENTS]) {
  int index = 0;
  int lowest = partials[index];

  while (index < NUMBER_OF_STUDENTS) {
    if (lowest > partials[index]) {
      lowest = partials[index];
    }

    index++;
  }

  printf ("\nEl promedio mas bajo es: %d", lowest);
}

/*!
 * @description: Renato's homework
 * @author: Michell Ayala <starships@outlook.com>
 * @version: 0.0.1
 */

int main(void)
{
  int student_number = 0;
  int asignature_number = 0;

  float partial_note = 0.0f;
  float final_exam = 0;

  char *student_name;

  char *names[NUMBER_OF_STUDENTS];

  float partials[NUMBER_OF_STUDENTS];
  float finals[NUMBER_OF_STUDENTS];

  int asignatures[NUMBER_OF_STUDENTS];

  while (student_number < NUMBER_OF_STUDENTS) {
    printf("[%d] Nombre del Estudiante: ", student_number);
    student_name = getString();

    printf("[%d] Promedio de Parciales: ", student_number);
    partial_note = getFloat();

    printf("[%d] Asignaciones: ", student_number);
    asignature_number = getFloat();

    printf("[%d] Examen Final: ", student_number);
    final_exam = getFloat();

    putchar('\n');

    names[student_number]  = student_name;
    partials[student_number] = partial_note;
    asignatures[student_number]  = asignature_number;
    finals[student_number]  = final_exam;

    student_number++;
  }

  calculate_average(partials, asignatures, finals);
  calculate_highest_partial(partials);
  calculate_lowest_partial(partials);

  return 0;
}