Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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 - Fatal编程技术网

C 如何打印数组中同一元素的下一个索引

C 如何打印数组中同一元素的下一个索引,c,C,我正在编写一个函数,在数组中查找两个相同的元素,然后打印它们的索引值。我似乎找不到正确的第二个索引。我有什么问题 代码: 我遇到麻烦的函数名为'couplesSearch',它返回 “打印对”的值。第139行 #包括 #包括 #定义NUM_来宾200 #定义FALSE 0 #定义真1 #定义12岁的孩子 浮动平均值(整数年龄[],整数大小); 国际餐(国际客人[],国际尺码); int搜索(int guestAges[],int size); 无效打印(int条件); int-coupleSea

我正在编写一个函数,在数组中查找两个相同的元素,然后打印它们的索引值。我似乎找不到正确的第二个索引。我有什么问题

代码:

我遇到麻烦的函数名为'couplesSearch',它返回 “打印对”的值。第139行

#包括
#包括
#定义NUM_来宾200
#定义FALSE 0
#定义真1
#定义12岁的孩子
浮动平均值(整数年龄[],整数大小);
国际餐(国际客人[],国际尺码);
int搜索(int guestAges[],int size);
无效打印(int条件);
int-coupleSearch(int-guestAges[],int-size);
无效打印对(int-size、int-condition2、int-ageFriend);
内部主(空)
{
int guestAges[NUM_GUESTS]={42、108、95、101、90、5、79、79、83、105、66、66、2、28、2、12、116、63、28、37、,
112, 85, 63, 34, 53, 23, 22, 117, 39, 96, 48, 7, 12, 19, 70, 113, 108, 20, 116,
55, 24, 52, 3, 94, 34, 105, 22, 32, 54, 29, 108, 45, 23, 118, 118, 20, 84, 22,
50, 59, 77, 36, 111, 43, 49, 107, 41, 63, 65, 89, 87, 46, 51, 10, 11, 111, 7, 22,
34, 69, 70, 24, 85, 35, 37, 81, 47, 57, 12, 29, 25, 40, 27, 44, 18, 59, 39, 43, 
10, 102, 34, 36, 80, 19, 25, 91, 100, 27, 114, 67, 102, 66, 45, 113, 31, 70, 18, 
94, 58, 73, 107, 91, 42, 37, 36, 48, 16, 95, 72, 53, 111, 71, 22, 5, 47, 71, 28, 
72, 8, 58, 98, 48, 34, 64, 66, 30, 50, 39, 102, 109, 63, 107, 27, 71, 94, 9,
61, 72, 43, 96, 11, 120, 25, 18, 69, 4, 116, 82, 3, 111, 92, 117, 15, 101, 37, 22, 
109, 40, 109, 5, 2, 55, 54, 80, 19, 99, 61, 69, 8, 108, 9, 14, 49, 44, 48, 22, 
31, 18, 14, 35};
int size=0;
printf(“平均值为:%.2f\n”,平均值(访客数量、大小));
printf(“12岁及以下儿童人数:%d\n”,膳食(客人、大小);
打印(搜索(访客、大小));
对联搜索(访客、大小);
/*printf(“索引%d和索引%d的两位16岁客人”);
printf(“最年轻客人年龄:”);
printf(“最老客人年龄:”)*/
返回0;
}
/*
函数将返回聚会客人的平均年龄
输入:年龄数组、来宾数
产出:平均年龄
*/
浮动平均值(int guestAges[],int size)
{
浮点数和=0;
浮动平均值=0;
用于(大小=0;大小

谢谢。

您的测试假设年龄数组中有两个条目。也就是说,寻找朋友的客人的年龄不是唯一的,并且数组已经排序。传入的数组不按年龄排序。因此,大小+2将不包含与大小+1相同的年龄

首先对年龄数组进行排序,然后在数组中搜索该年龄的第一个和最后一个条目

如果不想对数组进行排序,则必须测试数组中的每个条目,并跟踪匹配的索引

还可以在找到第一个匹配项后立即退出coupleSearch()。如果不希望排序和搜索第一个匹配项和最后一个匹配项,则需要分别搜索第二个匹配项


由于打印的大小为+1,因此打印的答案是基于1的。

您只搜索第一个匹配项。换句话说,你永远不会寻找一对

也许你应该试试这样:

void printCouples(int size, int condition2, int ageFriend)
{
    int j = size+1;  // Start after the first match
    int found = 0;

    while (j < NUM_GUESTS )
    {
        if (ageFriend == guestAges[j])
        {
            found = 1; 
            printf("Two guest with age %d at index %d and index %d\n", ageFriend, size, j);
        }
    }
    if (!found)
    {
        printf("No guest this age.\n");
    } 
}
void打印对(int-size,int-condition2,int-ageFriend)
{
int j=size+1;//在第一次匹配后开始
int=0;
while(j

请注意,
condition2
不再使用。

printpolds
通过打印
size+1、size+2来假定它们具有相邻的数组索引。为其他索引添加另一个参数。
void printCouples(int size, int condition2, int ageFriend)
{
    int j = size+1;  // Start after the first match
    int found = 0;

    while (j < NUM_GUESTS )
    {
        if (ageFriend == guestAges[j])
        {
            found = 1; 
            printf("Two guest with age %d at index %d and index %d\n", ageFriend, size, j);
        }
    }
    if (!found)
    {
        printf("No guest this age.\n");
    } 
}