0

The C program(main function)asks the user for the number of elements in an array(which this function works). The C function findSecondSmallest finds the second smallest element of an array(points toward the first index). The C function sortAscending arranges the array elements in ascending order (does not display the inputted elements, just garbage values) and the C function displayArray should display the arranged array in ascending order.

#include<stdio.h>
#include<limits.h>
int initializeArray(int num,int arr[]);
int findSecondSmallest(int num,int arr[]);
int *sortAscending(int num,int arr[]);
void displayArray(int num,int arr[]);
void main(){
    int array[50],*arr;
    int n = sizeof(array) / sizeof(array[0]);
    initializeArray(n,array);
    findSecondSmallest(n,array);
    arr = sortAscending(n,array);
    displayArray(n,arr);
}

int initializeArray(int num,int arr[]){
    int i;
    printf("Input the number of elements in an array: ");
    scanf("%d", &num);
    printf("\nEnter %d elements: ", num);
    for(i = 0;i < num;i++){
        scanf("%d", &arr[i]);
    }
}

int findSecondSmallest(int num,int arr[]){
    int i,secondSmall,small;
    small = secondSmall = INT_MAX;
    for(i = 1;i < num;i++){
        if(arr[i] < small){
            secondSmall = small;
            small = arr[i];
        }else if(arr[i] < secondSmall && arr[i] > small){
            secondSmall = arr[i];
        }
    }
    printf("The second smallest element is %d", secondSmall);
}


int *sortAscending(int num,int arr[]){
    int i,j,temp;
    for(i = 0;i < num;i++){
        for(j = i + 1;j < num;j++){
            if(arr[i] > arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}
void displayArray(int num,int arr[]){
    int i;
    for(i = 0;i < num;i++){
        printf("Element at Array[%d] = %d ", i, arr[i]);
    }
}

In need of help

9
  • 2
    The user is only entering the first num elements, but you're sorting the entire array, which includes all the garbage values that weren't filled in. Commented Sep 18, 2020 at 1:22
  • 2
    Have initialize_array return num, then use that when you call the other functions. Commented Sep 18, 2020 at 1:23
  • Why is findSecondSmallest starting from i = 1 instead of i = 0? Commented Sep 18, 2020 at 1:24
  • is it not i = 1 ? Commented Sep 18, 2020 at 5:10
  • Array indexes start at 0. Look at your loops in initializeArray and displayArray. Commented Sep 18, 2020 at 5:12

1 Answer 1

2

You need to process only the elements that the user input, not the entire array, since the rest of the array is uninitialized.

Change initializeArray() to return the number of elements that the user input. Then use that instead of n when calling the other functions.

initializeArray() wasn't using the parameter num. It should use that to check that the user doesn't enter more elements than the array can hold. So you need to use a different variable for the parameter and the user's input, so you can compare them.

findSecondSmallest() doesn't return anything, so it should be declared void, not int.

sortAscending() is supposed to return a pointer to the array, but it was missing the return statement.

In findSecondSmallest() you can initialize the variables to the first element of the array rather than INT_MAX. Then it makes sense to start your loop from i = 1, since you've already used element 0.

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

int initializeArray(int num,int arr[]);
void findSecondSmallest(int num,int arr[]);
int *sortAscending(int num,int arr[]);
void displayArray(int num,int arr[]);

int main(void){
    int array[50],*arr;
    int n = sizeof(array) / sizeof(array[0]);
    int num = initializeArray(n,array);
    findSecondSmallest(num,array);
    arr = sortAscending(num,array);
    displayArray(num,arr);
}

int initializeArray(int n,int arr[]){
    int i, num;
    printf("Input the number of elements in an array: ");
    scanf("%d", &num);
    if (num > n) {
        printf("That's too many elements\n");
        exit(1);
    }
    printf("\nEnter %d elements: ", num);
    for(i = 0;i < num;i++){
        scanf("%d", &arr[i]);
    }
    return num;
}

void findSecondSmallest(int num,int arr[]){
    int i,secondSmall,small;
    small = secondSmall = arr[0];
    for(i = 1;i < num;i++){
        if(arr[i] < small){
            secondSmall = small;
            small = arr[i];
        }else if(arr[i] < secondSmall && arr[i] > small){
            secondSmall = arr[i];
        }
    }
    printf("The second smallest element is %d\n", secondSmall);
}


int *sortAscending(int num,int arr[]){
    int i,j,temp;
    for(i = 0;i < num;i++){
        for(j = i + 1;j < num;j++){
            if(arr[i] > arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}

void displayArray(int num,int arr[]){
    int i;
    for(i = 0;i < num;i++){
        printf("Element at Array[%d] = %d\n", i, arr[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.