Tuesday, December 23, 2014

Split an Array from Specified in c pgm



Split an Array from Specified Position & Add First Part to the End
 
#include 
#include 
void main ()
{
    int number[30];
    int i, n, a, j;
    clrscr();
    printf("Enter the value of n\n");
    scanf("%d", &n);
    printf("enter the numbers\n");
    for (i = 0; i < n; ++i)
        scanf("%d", &number[i]);
    printf("Enter the position of the element to split the array \n");
    scanf("%d", &a);
    for (i = 0; i < a; ++i)
    {
        number[n] = number[0];
        for (j = 0; j < n; ++j)
        {
            number[j] = number[j + 1];
        }
    }
    printf("The resultant array is\n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", number[i]);
    }
     getch();
}
 
 
 
 
 
Output
 
Enter the value of n
4
enter the numbers
3
678
345
876
Enter the position of the element to split the array
3
The resultant array is
876
3
678
345

 


Sort the N Names in an Alphabetical Order
#include 
#include 
#include 
void main()
{
    char name[10][8], tname[10][8], temp[8];
    int i, j, n;
 
    printf("Enter the value of n \n");
    scanf("%d", &n);
    printf("Enter %d names n", \n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", name[i]);
        strcpy(tname[i], name[i]);
    }
    for (i = 0; i < n - 1 ; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(name[i], name[j]) > 0)
            {
                strcpy(temp, name[i]);
                strcpy(name[i], name[j]);
                strcpy(name[j], temp);
            }
        }
    }
    printf("\n----------------------------------------\n");
    printf("Input NamestSorted names\n");
    printf("------------------------------------------\n");
    for (i = 0; i < n; i++)
    {
        printf("%s\t\t%s\n", tname[i], name[i]);
    }
    printf("------------------------------------------\n");
    getch();
}

Output
 
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
 
----------------------------------------
Input Names    Sorted names
------------------------------------------
heap           class
stack          heap
queue          object
object         program
class          project
program        queue
project        stack
------------------------------------------
 
 
 

No comments:

Post a Comment