gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:51:45 25 4
gpt4 key购买 nike

我正在编写一个函数,该函数在数组中查找两个相同的元素,然后打印它们的索引值。我似乎无法获得正确的第二个索引。我的问题是什么?

代码:

the function I'm troubled with called 'couplesSearch' and it returns its value to 'printCouples'.line 139

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

#define NUM_GUESTS 200
#define FALSE 0
#define TRUE 1
#define KIDS_AGE 12

float averageAge(int ages[], int size);
int meals(int guestAges[], int size);
int search(int guestAges[], int size);
void print(int condition);
int coupleSearch(int guestAges[], int size);
void printCouples(int size, int condition2, int ageFriend);

int main(void)
{
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("average is: %.2f\n", averageAge(guestAges,size));
printf("Number of kids 12 and under: %d\n" , meals(guestAges, size));
print(search(guestAges,size));
coupleSearch(guestAges,size);
/*printf("Two guests with age 16 at index %d and index %d");
printf("Youngest guest age: ");
printf("Oldest guest age: ");*/
return 0;
}

/*
Function will return average age of party guests
input: age array, number of guests
output: average age
*/
float averageAge(int guestAges[], int size)
{
float sum = 0;
float avg = 0;

for(size = 0; size < NUM_GUESTS; ++size)
{
sum += guestAges[size];
}
avg = sum / NUM_GUESTS;
return avg;
}

int meals(int guestAges[], int size)
{
int kids = 0;
for (size = 0; size < NUM_GUESTS; size++)
{
if (guestAges[size] == KIDS_AGE || guestAges[size] < KIDS_AGE)
{
kids++;
}
}
return kids;
}

int search(int guestAges[], int size)
{
int condition = 0;
int age = 0;
printf("Enter age to search: ");
scanf("%d", &age);

while (size < NUM_GUESTS && age != guestAges[size])
{
size++;
}

if (size < NUM_GUESTS)
{
condition = TRUE;
} else
{
condition = FALSE;
}


return condition;
}

void print(int condition)
{
if (condition == TRUE)
{
printf("Guest found!\n");
}
else if (condition == FALSE)
{
printf("No guest this age.\n");
}
}

int coupleSearch(int guestAges[], int size)
{
int condition2 = 0;
int ageFriend = 0;
printf("Enter age of guest looking for friend: ");
scanf("%d", &ageFriend);

while (size < NUM_GUESTS && ageFriend != guestAges[size])
{
size++;
}

if (size < NUM_GUESTS)
{
condition2 = TRUE;
} else {
condition2 = FALSE;
}
printCouples(size,condition2,ageFriend);
return condition2;
}

void printCouples(int size, int condition2, int ageFriend)
{
if (condition2 == TRUE)
{
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size+1, size+2);
}
else if (condition2 == FALSE)
{
printf("No guest this age.\n");
}
}

谢谢。

最佳答案

您的测试假设年龄数组中有两个条目。即找 friend 的客人年龄不唯一且数组已排序。您传入的数组不按年龄排序。因此,size+2 不会包含与 size+1 相同的年龄。

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

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

一旦找到第一个匹配项,您也可以退出 CoupleSearch()。如果您不想排序和搜索第一个和最后一个匹配项,则需要单独搜索第二个匹配项。

由于您打印尺寸+1,因此您打印的答案是基于 1 的。

关于c - 如何打印数组中相同元素的下一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41599940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com