1.11M Members

inserting an element into an array in c language

 
0
 

hi
i am right now new to c language..and i have been facing problem in 1 array problem where i have to insert an element in to an array...i would like ur help plzz..i have tried everything but i m not able to make out from where i should start although i know the logic but i don't know what will be the code as i have not reached to any conclusion....so plz help me out .....

 
0
 

i have tried everything

But you forgot to copy and paste these attempts that we may correct them and instead assume that someone will drop in your lap exactly the code you are thinking of.

 
0
 
#include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a[11],i,j,k,ch,sh,e;
 clrscr();
 printf("enter array elements:");
 for(i=0;i<10;i++)
 scanf("%d",&a[i]);
 printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
 scanf("%d",&ch);
 switch(ch)
 {case1:
 printf("enter the element:");
 scanf("%d",&e);
 printf("enter the element after which the number has to be inserted:");
 scanf("%d",&sh);
 for(i=0;i<10;i++)
 {
 if(a[i]==sh)//finding the element
 break;
 }
 for(k=9;k>i;k--)
 a[k+1]=a[k];//shifting the element
 a[i+1]=e;
 break;
 case2:
 printf("enter the element:");
 scanf("%d",&e);
 for(k=9;k>=0;k--)
 a[k+1]=a[k];
 a[0]=e;
 break;
 case3:
 printf("enter the element:");
 scanf("%d",&e);
 a[10]=e;
 }
 printf("\n");
 for(i=0;i<10;i++)//display the result
 printf("%d",a[i]);
 getch();
 }

when i run this code...the output is like:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
enter the choice1.insertion in between 2.insertion in the beginning 3.insertion at the end
so when i enter choice 1.insertion in the beginning...it immediately prints:
enter the element:20
it does not print the line"enter the element after which the number has to be inserted"
and shows the unusual output of 1 2 3 4 5 6 7 8 9 10 20 52 4.....
now i don't seem to understand why is this program showing such kind of error while running?
plz help me out here........

 
0
 

Copy and paste directly to avoid typos.

case1:
case2:
case3:

And please use http://www.daniweb.com/techtalkforums/misc.php?do=bbcode#code code tags.
My test:

 enter array elements:1 2 3 4 5 6 7 8 9 10
 enter choice
 1.in between insertion
 2.insertion in the beginning
 3.insertion at the end    
 enter the element: 2
 enter the element after which the number has to be inserted:5

 1234526789

You're not running an un-fixed MSVC6 by chance?

Also, reading a number from the stdin has caveats. Enlighten yourself.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1043372399&id=1043284385#opt3

 
0
 

what is the error in this program?im still not able to find out?it is still giving me the same output of 12 3 4 5 6 7 8 9 10 52 30 ....what is the error here?

#include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a[11],i,j,k,ch,sh,e;
 clrscr();
 printf("enter array elements:");
 for(i=0;i<10;i++)
 scanf("%d",&a[i]);
 printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
 scanf("%d",&ch);
 switch(ch)
 {case1:
 printf("enter the element:");
 scanf("%d",&e);
 printf("enter the element after which the number has to be inserted:");
 scanf("%d",&sh);
 for(i=0;i<10;i++)
 {
 if(a[i]==sh)//finding the element
 break;
 }
 for(k=9;k>i;k--)
 a[k+1]=a[k];//shifting the element
 a[i+1]=e;
 break;
 case2:
 printf("enter the element:");
 scanf("%d",&e);
 for(k=9;k>=0;k--)
 a[k+1]=a[k];
 a[0]=e;
 break;
 case3:
 printf("enter the element:");
 scanf("%d",&e);
 a[10]=e;
 }
 printf("\n");
 for(i=0;i<11;i++)//display the result
 printf("%d",a[i]);
 getch();
 }

when i run this code...the output is like:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
enter the choice1.insertion in between 2.insertion in the beginning 3.insertion at the end
so when i enter choice 1.insertion in the between and other choices...it immediately prints:
enter the element:30
it does not print the line"enter the element after which the number has to be inserted"
and shows the unusual output of 1 2 3 4 5 6 7 8 9 10 52 33 30.....
now i don't seem to understand why is this program showing such kind of error while running?
plz help me out here....

 
0
 

plz tell me the errors in my program.....im not able to find out....the errors in my program...plz tell me what r the changes that i need to make in order to make the program run correctly for all the 3 cases?

 
0
 

You forgot to make it display properly - so the code has many typos. Nevertheless i will help by creating a c++ program to do the same, try and spot the similarities to yours:

#include <iostream>
using namespace std;

int main(void)
{
    int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // allow for extra insert (11 and not 10 items!)
    int num = 0; 
    int data;

    do
    {
        cout << "Where do you want to insert an element (0-10)"; // replace it with 1 option
        cin >> num; // no ascii check here, watch out!
    } while (num < 0 || num > 10); // ask until num is in range

    // the intended position is in num
    cout << "enter data to insert ";
    cin >> data;
    // the element is in data

    for(int i = 9; i > num - 1; i--)
        array[i + 1] = array[i]; // shift
    array[num] = data; // insert

    cout << "Final Array: ";
    for(int i = 0; i < 11; i++)
        cout << array[i] << ",";   

    return 0;
}

this program inserts at any location, allowing you to replace all three options with just option 2 (anywhere)

 
0
 

Deja vu, deja vu, the OP has some learning to do!

Why start two threads with the exact same question?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article