1

The problem is basically printing two arrays on an external array as right part and left part but although I can print one part, I can not print the other part. The scenario is that for example 5 elements on the left and 7 at the right. Then it reverses the right side and sticks it to the beginning of the array.

Here is the piece of code that I typed.

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int mainarray[] = {} , n , i , rank;

 printf("\nHow many element[10..2000]: ");
 scanf("%d", &n);
 printf("\nEnter the array elements : ");
 for(i=0; i<n; ++i)
 {
      scanf("%d" , &mainarray[i]);
 }
 printf("\nThe array is : ");
 for(i=0 ; i<n ; i++)
 {
     printf("%d ",mainarray[i]);
 }    
 printf("\n\nEnter the rank of the element : ");
 scanf("%d",&rank);
 printf("\n\nElement on rank %d is: %d",rank, mainarray[rank-1]);


 int leftarray[] = {} , rightarray[] = {};

 for(i=0; i<=rank ; i++)
 {
          leftarray[i] = mainarray[i];
 }
 printf("\n\nThe left array is : ");
 for(i=0 ; i<=rank ; i++)
 {
         printf("%d ", leftarray[i]);
 }

 for(i=0 ; i<n-rank-1 ;i++)
 {
              rightarray[i]=mainarray[i+rank+1];
 }
 printf("\n\nThe right array is : ");
 for(i=0 ; i<n-rank-1 ;i++)
 {
         printf("%d ", rightarray[i]);
 }

 int j=n-rank-2,temp;
 i=0;
 while(i<j)
 {
           temp=rightarray[i];
           rightarray[i]=rightarray[j];
           rightarray[j]=temp;
           i++;
           j--;
 }
 printf("\n\nNew right array is : ");
 for(i=0 ; i<n-rank-1 ; i++)
 {
         printf("%d ", rightarray[i]);
 }

 i=0;

 while(i<n-rank-1)
 {
                mainarray[i]=rightarray[i];
                i++;
 }
 j=0;
 while(j<rank+1 && i<n-1)
 {
           mainarray[i]=leftarray[j];
           j++;
           i++;
 }

 printf("\n\nThe result is : ");
 for(i=0 ; i<n ; i++)
 {
         printf("%d ",mainarray[i]);
 }    

 printf("\n\n\n");
 system("pause");
 return 0;
}    
4
  • 1
    int mainarray[] = {} - ouch. if you were shooting for an array that can hold no elements, you found it. Commented Oct 28, 2016 at 16:37
  • 1
    When you do int mainarray[] = {} you define mainarrayas an empty array. All indexing of it will be out of bounds. Commented Oct 28, 2016 at 16:37
  • 1
    scanf("%d", &n); int mainarray[n]; Commented Oct 28, 2016 at 16:38
  • How can I create an array without declaring the size? @Someprogrammerdude Commented Oct 28, 2016 at 16:43

1 Answer 1

1

You're declaring empty arrays when you write:

int mainarray[] = {};

As a result, you're storing outside the array bounds, which results in undefined behavior.

You need to declare the arrays after you get the size from the user.

scanf("%d", &n);
int mainarray[n]; // no need to initialize here, you're going to fill it in with the input loop

Then do similar things for leftarray and rightarray:

scanf("%d",&rank);
int leftarray[rank];
int rightarray[n-rank];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @Barmar the problem will be solved after some tiny changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.