Hello can you help me please... im making the program that will display the smallest number using function in array...please help me...thank you in advance...hoping for your positive responds....

#include<stdio.h>

void accept();
void findsmallest(int num[],int size);
int a;
int main(void)
 {

   accept();
   getch();
   return 0;
 }

 void accept()
  {
    int size=0;
    int num[]=size;
    scanf("%d",size);


   for(a=0;a<size;a++)
    {
      scanf("%d",& num[a]);
      if(num[a]<small)
       {
	 num[a]=small;
	}
     }
   }

 void findsmallest(num,size);
  {
    return;
  }
}

Dani AI

Generated

The original snippet has a few root causes that stop it from working: array declared incorrectly, scanf called without an &, uninitialized variables, the logic updates the array instead of the "smallest" variable, findsmallest is never used, and allocation/parameter passing is mishandled. already pointed out the wrong assignment; tried a dynamic approach but allocated into a local pointer (that does not propagate back to main).

A clear, robust pattern:

  • Read and validate the size in main.
  • Allocate (or let main allocate) and fill the array, checking scanf and malloc results.
  • Implement a small, single-responsibility find_smallest(const int *arr, int n) that initializes the minimum to arr[0] and scans the rest.
  • Print the result and free memory. Avoid globals and nonstandard getch().

Example (concise, complete, and different from earlier posts):

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

int *read_numbers(int n) {
    int *a = malloc(n * sizeof *a);
    if (!a) return NULL;
    for (int i = 0; i < n; ++i)
        if (scanf("%d", &a[i]) != 1) { free(a); return NULL; }
    return a;
}

int find_smallest(const int *a, int n) {
    int min = a[0];
    for (int i = 1; i < n; ++i) if (a[i] < min) min = a[i];
    return min;
}

Extra tips: always check scanf/malloc return values, compile with -Wall -Wextra and fix warnings, and use valgrind to catch leaks. If you want accept to allocate memory for the caller, either return the pointer or accept an int ** parameter — do not rely on changing a local copy of a pointer.

Recommended Answers

All 3 Replies

Write your accept function this way
I have separated the data input and the smallest number calculation into 2 steps but you dont have to

void accept(int *a)
{
     int size =0,i=0;
     
     printf("Enter the size of the array\n");
     scanf("%d",&size);
     
     a = (int*)malloc(sizeof(int)*size);       // Size of the i/p array is dynamic
     
     for(i=0;i<size;i++)
     {
         printf("Enter the next integer\n");
         scanf("%d",&a[i]);
     }
     
     printf("The entered numbers are \n");
     
     for(i=0;i<size;i++)                   // Just for checking
         printf("%d\n",a[i]);
}          

int 
smallest(int* a)
{
     // Logic for finding the smallest number         
     return 0;
}

int
main(int argc, char *argv[])
{
          int *a;
          accept(a);     
          
          printf("The smallest number is %d\n",smallest(a));        
         
         free(a);
         return 0;
}

Hello can you help me please... im making the program that will display the smallest number using function in array...please help me...thank you in advance...hoping for your positive responds....

#include<stdio.h>

void accept();  //should be void accept(void);
void findsmallest(int num[],int size);  //well done!
int a;           //should be removed
int main(void)
 {

   accept();
   getch();
   return 0;
 }

 void accept()
  {
    int size=0, a;    //a goes down here
    int num[]=size;
    scanf("%d",size);   //should be &size, not just size

     
   for(a=0,small=num[a];a<size;a++) //assign small a value
    {                               //unassigned local variables   
      scanf("%d",&num[a]);          //could have any value    
      if(num[a]<small)              //to start with    
       {
	 num[a]=small;     //wrong! see below
	}
     }
   }

 void findsmallest(num,size);
  {
    return;
  }
}

I'm trying to be nice here, but this program is making it tough. It never even calls findsmallest (which has ZERO code in it, BTW. The code for it is up in the accept function (where it should be for efficiency), but the logic is wrong:

if(num[a]<small)
       {
     num[a]=small;     //wrong -- remove this line

     small = num[a];  //correct -- use this one.
    }

If you want to find the smallest value, in the findsmallest function, I have no problems with that. Just remove the if statement from the accept function, and put it into the findsmallest. You'll also need to have a separate for loop for it, in findsmallest.

Then, just call findsmallest like the prototype shows, and be sure to print out the smallest value.

There are LOTS of C tutorials on the web, some video tutorials from major colleges and universities. There are LOTS of programs on the web that you can look at.

Please study up, and work on your programs. You're wrecking my blood pressure!! ;)

I'm trying to be nice here, but this program is making it tough. It never even calls findsmallest (which has ZERO code in it, BTW. The code for it is up in the accept function (where it should be for efficiency), but the logic is wrong:

if(num[a]<small)
       {
     num[a]=small;     //wrong -- remove this line

     small = num[a];  //correct -- use this one.
    }

If you want to find the smallest value, in the findsmallest function, I have no problems with that. Just remove the if statement from the accept function, and put it into the findsmallest. You'll also need to have a separate for loop for it, in findsmallest.

Then, just call findsmallest like the prototype shows, and be sure to print out the smallest value.

There are LOTS of C tutorials on the web, some video tutorials from major colleges and universities. There are LOTS of programs on the web that you can look at.

Please study up, and work on your programs. You're wrecking my blood pressure!! ;)

hello sir i have some question about in your post why is it that some of your post can't be seen the only that i can see is this one

if(num[a]<small)
       {
     num[a]=small;     //wrong -- remove this line

     small = num[a];  //correct -- use this one.
    }

i tried to copy this page to the desktop then i open i saw your some post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.