Hello all,
New to programming.
I have an assignment where I have to do the following:
Read in teacher's first and last names;
read in their salaries;
find the average of the salaries;
find the highest and lowest salaries
The program runs well until it get to the lowest salary; where is prints/displays gibberish.
Totally baffled.
Code is written in Visual Studio Community 2015
Thanks
ps Will be more than happy to accept tips on writing cleaner code.
-------------------------------------------------------------------
Code:
#define _CRT_SECURE_NO_WARNINGS
#define LEN 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
//set variables for teachers first and last names.
char teacherFirstName[50][LEN];
char teacherLastName[50][LEN];
//set variable for teacher salary.
int teacherSalary[50];
//set variable for user input
int howManyTeachers = 0;
//set variables for teachers first and last names.
char teacherFirstName[50][LEN];
char teacherLastName[50][LEN];
//set variable for teacher salary.
int teacherSalary[50];
//set variable for user input
int howManyTeachers = 0;
int i = 0;
int avg = 0;
int sum = 0;
int topSalary = 0;
int bottomSalary = 0;
printf("Enter teachers\n");
scanf("%d", &howManyTeachers);
//for loop to read in teacher's names and salaries
for (i = 0; i<howManyTeachers; i++) {
printf("\nteachers first name :");
scanf("%s", &(teacherFirstName[i]));
printf("\nteachers last name: ");
scanf("%s", &(teacherLastName[i]));
printf("\nteachers salary: ");
scanf("%d", &(teacherSalary[i]));
sum += teacherSalary[i];
}
// for loop to display teacher's names and salaries.
for (i = 0; i<howManyTeachers; i++) {
printf("\n\nTeacher %d: ", i + 1);
printf("%s", strcat(teacherFirstName[i], teacherLastName[i]));
printf("\tSalary per year:");
printf("%d", teacherSalary[i]);
}
//formula to calculate averag of teacher's salaries.
avg = (float)sum / howManyTeachers;
//print the averages
printf("\n\nThe average salary is:");
printf("%d", avg);
topSalary = teacherSalary[i];
for (i = 0; i < howManyTeachers; i++) {
if (teacherSalary[i] > topSalary) {
topSalary = teacherSalary[i];
}
}
bottomSalary = teacherSalary[i];
for (i = 0; i < howManyTeachers; i++) {
if (teacherSalary[i] < bottomSalary) {
bottomSalary = teacherSalary[i];
}
}
printf("\n\nThe top salary is:");
printf("%d", topSalary);
printf("\nThe bottom salary is:");
printf("%d", bottomSalary);
system("pause");
return 0;
}//end of main function.