Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法确定一个值是否在数组中以及它在C中的位置?_C_Arrays_Function - Fatal编程技术网

有没有办法确定一个值是否在数组中以及它在C中的位置?

有没有办法确定一个值是否在数组中以及它在C中的位置?,c,arrays,function,C,Arrays,Function,我是C语言的新手,我的任务是创建一个函数,该函数使用数组来获取不同的GPA值,并用它们进行各种计算。所需的任务之一包括确定值是否存储在数组中及其在数组中的位置。我只是想知道最好的方法是什么?我发现前面回答的问题可以确定数组中是否有对象,但不能确定其位置 完整代码如下: #include <stdio.h> #define SIZE 1000 //define max # of GPAs to be input void mainFunction() { //creat

我是C语言的新手,我的任务是创建一个函数,该函数使用数组来获取不同的GPA值,并用它们进行各种计算。所需的任务之一包括确定值是否存储在数组中及其在数组中的位置。我只是想知道最好的方法是什么?我发现前面回答的问题可以确定数组中是否有对象,但不能确定其位置

完整代码如下:

#include <stdio.h>


#define SIZE 1000 //define max # of GPAs to be input



void mainFunction()
{

  //create array variables
  int x = 0.0;
  float gpas[1000] = {0};


  //used in switch
  char switchLetter;


  //used for calculations
  float sum = 0.0;
  float average;
  int counter =0;


  // used for highest/lowest calculations
  float lowest, highest, buf;


  //used for arranging array in ascending order
  int a, j;


  //used for determining if a value is in an array
  float value;


  //create menu
  printf("\n");
  printf("************************************\n");
  printf("**           MAIN MENU            **\n");
  printf("************************************\n");
  printf("A. Enter GPA\n");
  printf("B. Display Average of all GPA's\n");
  printf("C. Display Highest GPA\n");
  printf("D. Display Lowest GPA\n");
  printf("E. Display the adjusted average\n");
  printf("F. See if a certain GPA was entered\n");
  printf("G. Display the contents of the array\n");
  printf("Q. Quit\n");
  printf("************************************\n");



  //create switch statement 
  do{
  //used to control switch
  scanf("%c", &switchLetter);


  switch(switchLetter)
  {
//*********************************************************//
    case 'A':
    for(x=0; x<1000; x++)
      {
      scanf("%f", &gpas[x]);
      counter++;
      sum += gpas[x];
          if (gpas[x] >= 3.5)
          {
            printf("\nNice Work!\n");
          }

          if (gpas[x] <= 2.0)
          {
            printf("\nYou need to study harder :() \n");
          }
      }

            for (x = 0; x < SIZE; ++x) 
              {

            for (j = x + 1; j < SIZE; ++j)
            {
             if (gpas[x] > gpas[j])
             {
             a =  gpas[x];
              gpas[x] = gpas[j];
              gpas[j] = a;
             }
             }
              }          
    break;
//*********************************************************//
    case 'B':
    {
      sum += gpas[x];
      average = (sum / counter);
      printf("\nAverage = %.2lf\n", average);
      break;
    }
//*********************************************************//
    case 'C':
    {
      highest = gpas[0];

      for (x = 1; x < SIZE; x++) 
        {
          if (highest < gpas[x])
            highest = gpas[x];
          }

      printf("\nhighest = %.2lf\n", highest);
      break;
    }
//*********************************************************//
    case 'D':
    {
      lowest = gpas[0];

      for (x =1; x < SIZE; x++)
        {
          if (lowest > gpas[x])
          lowest = gpas[x];
        }
         printf("\nlowest = %.2lf\n\n", lowest);
         break;
    }
//*********************************************************//
    case 'E':
    {

      gpas[x] = (gpas[x] - gpas[0]);
      sum += gpas[x];
      average = (sum / counter);
      printf("\nAverage = %.2lf\n", average); 
      break;
    }    
//*********************************************************//
    case 'F':
    {
      printf("Enter Value:\n");
      scanf("%f", &value);
      if (value == gpas[x])
      {
        printf("\ntrue\n");
      }
      break;
    }
//*********************************************************//
    case 'G':
    printf("Contents of Array: \n");
    for(x=0; x<counter; x++)  
      {  
      printf("%.2f, ", gpas[x]);  
      } 
      break;
//*********************************************************//
    case 'Q':
    return;
  }

  }
while (x<= SIZE);
}



int main(void) 
{
  mainFunction();
}

#包括
#定义大小1000//定义要输入的GPA的最大值
void main函数()
{
//创建数组变量
int x=0.0;
浮点GPA[1000]={0};
//用于开关
字符转换字母;
//用于计算
浮动总和=0.0;
浮动平均;
int计数器=0;
//用于最高/最低计算
浮动最低,最高,buf;
//用于按升序排列数组
int a,j;
//用于确定值是否在数组中
浮动值;
//创建菜单
printf(“\n”);
printf(“***************************************************\n”);
printf(“**主菜单**\n”);
printf(“***************************************************\n”);
printf(“A.输入GPA\n”);
printf(“B.显示所有GPA的平均值\n”);
printf(“C.显示最高GPA\n”);
printf(“D.显示最低GPA\n”);
printf(“E.显示调整后的平均值\n”);
printf(“F.查看是否输入了某个GPA”\n”);
printf(“G.显示数组的内容\n”);
printf(“Q.Quit\n”);
printf(“***************************************************\n”);
//CREATESWITCH语句
做{
//用于控制开关
scanf(“%c”、&switchLetter);
开关(开关字母)
{
//*********************************************************//
案例“A”:
对于(x=0;x=3.5)
{
printf(“\n临时工作!\n”);
}
if(gpas[x]gpas[j])
{
a=gpas[x];
gpas[x]=gpas[j];
gpas[j]=a;
}
}
}          
打破
//*********************************************************//
案例“B”:
{
总和+=gpas[x];
平均值=(总和/计数器);
printf(“\nAverage=%.2lf\n”,平均值);
打破
}
//*********************************************************//
案例“C”:
{
最高=平均分[0];
对于(x=1;xgpas[x])
最低=gpas[x];
}
printf(“\nlowest=%.2lf\n\n”,最低);
打破
}
//*********************************************************//
案例“E”:
{
gpas[x]=(gpas[x]-gpas[0]);
总和+=gpas[x];
平均值=(总和/计数器);
printf(“\nAverage=%.2lf\n”,平均值);
打破
}    
//*********************************************************//
案例“F”:
{
printf(“输入值:\n”);
scanf(“%f”和“值”);
如果(值==gpas[x])
{
printf(“\ntrue\n”);
}
打破
}
//*********************************************************//
案例“G”:
printf(“数组的内容:\n”);

对于(x=0;x而言,一种方法是:

int findElement(int array[], int arraySize, int gpa)
{
  for (int i = 0; i < arraySize; i++)
  {
     // If you find the element, return its index
     if (array[i] == gpa) return i;
  }
  // If no index was found, return -1
  return -1;
}
int-findElement(int-array[],int-arraySize,int-gpa)
{
for(int i=0;i

由于数组不能有负索引,返回-1将表示找不到元素。任何其他值都将表示找到了元素,以及它的索引是什么。

数组是否排序?根据定义,如果您在数组中发现了一个项,则您已确定该发现所在的位置。您无法获取您如何知道
scanf(“%f”和&gpas[x]);
成功?(或对
scanf
的任何其他调用)?如果输入中有错误的字母字符,会发生什么情况?您如何知道是否发生了匹配失败?在匹配失败的情况下,
gpas[x]的值是多少
hold?.故事的寓意-
scanf
(或任何其他输入函数)无法正确使用,除非您检查每个输入的返回值。例如
if(scanf(“%f”)、&gpas[x])!=1{fputs(“错误:无效浮点输入。\n”,stderr);exit(exit_FAILURE);}
“因为数组不能有负索引”-是的,这是正确的,但是可以在指针上使用负索引,该指针是应用了任意偏移量的衰减数组的结果。换句话说,
array[-1]
在这种情况下可能是有效的。是的。我在打字时考虑过这一点,但他说他对c比较陌生,所以我觉得不值得一提。这可能是我的错误。我必须调整代码,但效果很好!非常感谢!