Reputation:
I am having two minor problems in this code but i am unable to get them. i have mentioned on the places the compiler is giving error.There are two of them given below:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 100
int count;
void menu(void);
void input(int[]);
void print(int[]);
void insert(int[]);
void delete_element(int arr[]);
void search(int arr[]);
void main(void)
{
int arr[SIZE];
char choice;
while(1)
{
menu();
choice=getche();
switch (choice)
{
case'1':input(arr);break;
case'2':delete_element(arr);break;
case'3':insert(arr);break;
case'4':print(arr);break;
case'5':search(arr);break;
case'6':exit(0);//from stdlib.h
default:printf("Enter valid choice!");
}
getch();
}
void print(int arr[])
{ // says declaration syntax error here
int i;
for(i=0;i<count ;i++)
printf("element is %d",arr[i]);
}
void input(int arr[])
{
if(count<SIZE)
for(count=0; ;count++)
{
printf("Enter element %d:",i+1);
scanf("%d"&arr[i]);
if(arr[count]==0)
{
count--;
break;
}
}
}
void insert(int arr[])
{
int i,value,index;
if(count==SIZE)
printf("Not enough space to perform insertion");
else
{
printf("Enter value and index:");
scanf("%d",&value,&index);
for(i=index;i<=LEN;i++)
{
arr[i]=arr[i-1];
}
arr[index]=value;
count++;
printf("insertion succesful");
}
}
void delete_element(int arr[])
{
int index,i;
if(count==0)
{
printf("Empty array");
}
else
{
printf("Enter Index:");
scanf("%d",&index);
for(i=index;i<LEN;i++)
{
arr[i]=arr[i+1]
}
count--;
printf("Delete succesful.");
}
}
void search(int arr[])
{
int value,flag=0,i;
printf("Enter value:");
scanf("%d",&value);
for(i=0;i<count;i++)
{
if(arr[i]==value)
{
printf("Value %d is found at index:",value,index);
flag=i;
}
}
if(!flag)
printf("Value not found");
printf("Search Complete");
}
} // declaration missing ; here
Upvotes: 0
Views: 3949
Reputation: 9866
For one, you are missing a semicolon here:
for(i=index;i<LEN;i++)
{
arr[i]=arr[i+1] // Missing semicolon!
}
You also did not close your main
function with a right curly brace. Move the curly brace at the end of the program to before your implementation of print()
.
One more point about readability. Make sure to indent after open braces and inside if
statements. The following lines are confusing as it's unclear that only the first printf
is part of the if
statement.
if(!flag)
printf("Value not found");
printf("Search Complete");
Instead, indent the second line, and for even more clarity, you might want to use braces. LIke so:
if(!flag)
{
printf("Value not found");
}
printf("Search Complete");
Upvotes: 7
Reputation: 285047
You're declaring functions within main, which is incorrect. Remove the last right curly brace, and insert one before void print
. You should format your code in a consistent and readable way.
Upvotes: 3