tags:

views:

111

answers:

2

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
+3  A: 

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.

Matthew Flaschen
+7  A: 

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");
Justin Ardini
Also, replace `printf("Value %d is found at index:",value,index);` with `printf("Value %d is found at index %d",value,i);`
Yktula
Meh, when people say 'your code is unreadable', they mean 'your code is unreadable to *me*'. In the end it's about conventions and what you're used to, a common example is text formatted without commata or punctuation, people tend to fail to see that the people that write like that can read it quite easily, because they're used to it. Let's not forget that about 95% of the scripts in world history didn't use any form of punctuation.People that don't indent or always use braces can perfectly read their code as it, of course, for most projects, code also needs to be read by others...
Lajla
and define LEN, which comes up at `for(i=index;i<LEN;i++)`
Yktula
@Lajla: Exactly, it's about consistency and following convention. If this is homework that needs to be handed in, I'm sure the reader would appreciate following C indentation conventions.
Justin Ardini
I disagree with @Lajla. If this was a case where the code was consistent and readable, the question wouldn't be posted here about being unable to find compiler errors.Yes, everybody has their own style. Yes, some are gross. But any competent programmer can read them. If you can't read your own code, then following some tips to make it easier to debug might not hurt you.
Jon L
@Justin, I agree, but only if code is to be read by others. I personally have a tendency to ignore whitespace when I read (Which makes Python a nightmare as I find myself counting the spaces to make sense of it.) but I just use an automated formatter if code is meant to be read by others.I really don't believe that code, or anything really is more readable by nature, it is by nurture due to conventions adhered to. People used to think that serif fonts were more readable, then they found out it was because people were used to them...
Lajla
@Jon L We all have lost semicolons regardless of our formatting or indent style, I mean, the OP is probably relatively new to C if it involves homework, which means a trouble reading it and understanding the error message regardless of formatting style.Also, new programmers tend to ignore formatting style and only later see its benefit, could that be because if you haven't been exposed to the convention, it doesn't make it more readable to you? When I first started programming, it was inconsistent and it didn't matter for me, it was all not that readable, it takes time to get used to any style
Lajla