如何在我的c程序中定义起始索引?

时间:2014-12-20 00:10:06

标签: c arrays

我正在处理某个程序,但目前要求您输入值。

ex.

    How many number you want to enter?
    5

    Type in the numbers.
    1 2 3 4 5

但我想预先定义数字;

  

int i [5] = {1,2,3,4,5}

怎么做?

这是代码。

#include <stdio.h>
#include <conio.h>

void main ()
{
 int number[30];
 int i,n,a,j;

 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();
}

2 个答案:

答案 0 :(得分:0)

您是否有要插入的数字的最大限制? 如果是,您可以使用预定义的数字初始化您的使用数组,例如:

int predefined[5] = {1, 2, 3, 4, 5};

并在需要时替换值。

答案 1 :(得分:-1)

#include <stdio.h>
#include <conio.h>

int main ()
{

  int number[5]={1,2,3,4,5};
  int i,n=5,a,j;
  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();
  return 0;
}