C 我的程序没有';t输出在数组中搜索时输入的第一个值。为什么?
例如,如果我为数组写入1,2,3,4,5,然后再次写入1,2,3,4,5以在数组中搜索,它不会输出1。。。我很困惑。你能帮我吗C 我的程序没有';t输出在数组中搜索时输入的第一个值。为什么?,c,C,例如,如果我为数组写入1,2,3,4,5,然后再次写入1,2,3,4,5以在数组中搜索,它不会输出1。。。我很困惑。你能帮我吗 #include <stdio.h> #include <stdlib.h> int main() { int i, b, c, n, a; int ara[200]; int number[200]; printf("Please enter how many numbers you wanna input: \n&
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, b, c, n, a;
int ara[200];
int number[200];
printf("Please enter how many numbers you wanna input: \n");
scanf("%d", &n);
if (n > 200) {
// i limited the numbers users can input.
printf("You can't write higher than 200. It's out of range. ");
} else {
printf("Please write %d numbers: \n", n);
}
// i asked for numbers from user.
for (int i = 0; i < n; ++i) {
scanf(" %d", &number[i]);
}
// I asked for a number to search from the inputted array.
printf("Please enter numbers to search: \n");
scanf("%d", &a);
n = n - 1;
for (int a = 0; a < n; ++a) {
scanf("%d", &ara[a]);
}
n = n + 1;
for (i = 0; i < n; ++i) { // my program looks every line to find the number.
for (a = 0; a < n; ++a) {
// if program can find a number equals to the ones in array, it will print
// the line.
if (number[i] == ara[a]) {
// i wrote i+1 because normally number 1 is considered as number 0 in
// arrays.
printf("%d is at position %d in the array.\n", ara[a], i + 1);
// break; //to stop the loop.
}
}
}
}
#包括
#包括
int main(){
int i,b,c,n,a;
国际原子能机构[200];
整数[200];
printf(“请输入您要输入的数字:\n”);
scanf(“%d”和“&n”);
如果(n>200){
//我限制了用户可以输入的数量。
printf(“你不能写超过200,超出范围。”);
}否则{
printf(“请填写%d个数字:\n”,n);
}
//我向用户询问电话号码。
对于(int i=0;i
您的程序未定义为读取未初始化的内存。我可以证明,无论这个程序的输入是什么,它最终都会读取ara[n-1]
,由于行n=n-1,它从未初始化过代码>和n=n+1代码>。如果编译器成功地完成了这个归纳,它可以删除整个嵌套的for
循环,因为现代编译器假定未定义的行为意味着无法访问的代码
此外,此扫描行错误:scanf(“%d”和&a)代码>读取a
不起作用。读入n
也是如此。您需要另一个整数来存储搜索数组中的数字数量。你不能同时使用n
。我已经在你的代码上修改了几行,现在它可以正常工作了请自己检查:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, b, c, n, a;
int ara[200];
int number[200];
printf("Please enter how many numbers you wanna input: \n");
scanf("%d", &n);
if (n > 200) {
// i limited the numbers users can input.
printf("You can't write higher than 200. It's out of range. ");
} else {
printf("Please write %d numbers: \n", n);
}
// i asked for numbers from user.
for (int i = 0; i < n; ++i) {
scanf(" %d", &number[i]);
}
// I asked for a number to search from the inputted array.
printf("Please enter numbers to search: \n");
// scanf("%d", &a); // Your major problem was beginning from here.
for (int a = 0; a < n; ++a) {
scanf("%d", &ara[a]);
}
for (i = 0; i < n; ++i) { // my program looks every line to find the number.
for (a = 0; a < n; ++a) {
// if program can find a number equals to the ones in array, it will print
// the line.
if (number[i] == ara[a]) {
// i wrote i+1 because normally number 1 is considered as number 0 in
// arrays.
printf("%d is at position %d in the array.\n", ara[a], i + 1);
// break; //to stop the loop.
}
}
}
}
更新以提高可读性和效率
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int i, b, c, n, a;
int ara[200];
int number[200];
printf("Please enter how many numbers you wanna input: ");
scanf("%d", &n);
if (n > 200) {
// i limited the numbers users can input.
printf("You can't write higher than 200. It's out of range. ");
} else {
printf("Please write %d numbers:\n", n);
}
// i asked for numbers from user.
for (int i = 0; i < n; ++i) {
printf("%d. number: ",i+1);
scanf(" %d", &number[i]);
}
// First ask how many numbers the user wants to search.
printf("\nHow many numbers will be searched?: ");
scanf("%d", &a);
for (int s = 0; s < a; ++s) {
printf("%d. number to search: ",s+1);
scanf("%d", &ara[s]);
}
/* A better approach is to iterate in the outer loop for each number to be searched
* instead of iterateing for the numbers in the list.
*/
bool found;
for (i = 0; i < a; ++i) { // In the outer loop iterate for the numbers to be searched.
found = false; // Set found to false for each new number to be searched
for (b = 0; b < n; ++b) { // In the inner loop iterate for the list
// if program can find a number equals to the ones in array, it will print
// the line.
if (number[b] == ara[i]) {
// i wrote i+1 because normally number 1 is considered as number 0 in
// arrays.
printf("%d is at position %d in the array.\n", ara[i], b + 1);
found = true; // set found to true if you find it
}
}
if(!found){
printf("The number <%d> does not exist in the list\n",ara[i]);
}
}
}
#包括
#包括
#包括
int main(){
int i,b,c,n,a;
国际原子能机构[200];
整数[200];
printf(“请输入您要输入的数字:”);
scanf(“%d”和“&n”);
如果(n>200){
//我限制了用户可以输入的数量。
printf(“你不能写超过200,超出范围。”);
}否则{
printf(“请填写%d个数字:\n”,n);
}
//我向用户询问电话号码。
对于(int i=0;i
更新的代码输出如下:
[kozmotronik@kozmotronik-ge600nc0nd Belgeler]$ ./karalama
Please enter how many numbers you wanna input:
4
Please write 4 numbers:
3
5
3
7
Please enter numbers to search:
3
4
5
7
3 is at position 1 in the array.
5 is at position 2 in the array.
3 is at position 3 in the array.
7 is at position 4 in the array.
Please enter how many numbers you wanna input: 8
Please write 8 numbers:
1. number: 100
2. number: 2345
3. number: 457
4. number: 234
5. number: 2
6. number: 657
7. number: 4
8. number: 900
How many numbers will be searched?: 4
1. number to search: 4
2. number to search: 23
3. number to search: 657
4. number to search: 15
4 is at position 7 in the array.
The number <23> does not exist in the list
657 is at position 6 in the array.
The number <15> does not exist in the list
请输入您要输入的数字:8
请写8个数字:
1.电话:100
2.电话:2345
3.电话:457
4.电话:234
5.编号:2
6.电话:657
7.电话:4
8.电话:900
将搜索多少个数字?:4
1.搜索号码:4
2.搜索号码:23
3.搜索号码:657
4.搜索号码:15
4位于阵列中的位置7。
列表中不存在该编号
657位于阵列中的位置6。
列表中不存在该编号
非常感谢你,我是个新手,所以我迷路了,我正在努力学习。正如你所说,我重新安排了“数字搜索部分”,并放置了新的整数来存储我的值,现在它开始工作了。这真的很有帮助!非常感谢你!作为初学者,我很难看出自己的错误。这么有帮助的回答!您的代码中有一个更新。过来看!