Thread: Arrarys Finding Max and Min Integer

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    5

    Arrarys Finding Max and Min Integer

    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.

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Code:
    topSalary = teacherSalary[i];
    What is the value of i here?

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    5
    Thanks for the reply.

    My best understanding is "i" would represent the sum of the teachers salary. I could be and probably wrong.

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Code:
    // 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]);
    }
    //...
    topSalary = teacherSalary[i];
    Again, what is the value of i here? : )

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #define _CRT_SECURE_NO_WARNINGS
     
     
    #define LEN 100
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    Code safety tip move your local defines after the header includes that you DO NOT wish to change.

    I would do it like the below code.
    Code:
    #define _CRT_SECURE_NO_WARNINGS
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LEN 100
    Tim S.
    Last edited by stahta01; 10-23-2016 at 09:57 AM. Reason: typo
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting two dimensional arrarys
    By omishompi in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 12:05 PM
  2. Finding # of digits in an integer
    By Canucklehead in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2005, 12:54 AM
  3. Finding the Mode of an integer array
    By -]SC[-Dargoth in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 02:27 PM
  4. finding an integer in an integer
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 07:25 PM
  5. converting ints to arrarys
    By deleeuw in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2001, 02:31 PM

Tags for this Thread