I wrote this simple bubble sort program using dynamic memory allocation. I am using VC++ compiler.
// bubble_sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int a[],int n);
int main()
{
int *p,i;
int n;
printf("Enter number of array elements\n");
scanf("%d",&n);
p=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
bubble_sort(p,5);
printf("Sorted elements\n");
for(i=0;i<n;i++)
printf("%d ",p[i]);
free(p);
system("pause");
return 0;
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
What is wrong in above program? Compiler shows following warnings. What does it mean?
Warning 1 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Warning 2 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Please help me.
This is not the problem with your program. microsoft deprecated scanf function instead they introduce scanf_s function, which means they introduced security. To make compile your code there are two options.