我想比较两个不同的数组,它们都是int. 第一个数组是静态的,包含 1 到 10 的数字,第二个数组要求用户输入十个不同的数字,程序会检查两个数组中的哪些元素相等。
#include <stdio.h>
int main(void) {
int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int array2[10];
int i;
for (i = 0; i < 11; i++) {
printf("Enter numbers: ");
scanf("%d", &array2);
}
for (i = 0; i < 11; i++) {
if (array1[i] != array2[i]) {
printf("Not equal \n");
} else {
printf("They are equal. \n");
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使我输入的数字等于存储在第一个数组中的数字,程序也总是说不相等。
我正在创建一个程序,它从一个文本文件中读取数据并将其大小更改为大写或小写,然后将该数据存储在新文件中.我搜索过互联网,但我找不到如何创建新的文本文件.
#include <stdio.h>
int main(void) {
FILE *fp = NULL;
fp = fopen("textFile.txt" ,"a");
char choice;
if (fp != NULL) {
printf("Change Case \n");
printf("============\n");
printf("Case (U for upper, L for lower) : ");
scanf(" %c", &choice);
printf("Name of the original file : textFile.txt \n");
printf("Name of the updated file : newFile.txt \n");
Run Code Online (Sandbox Code Playgroud)
我知道这是不完整的,但我无法弄清楚如何创建一个新的文本文件!
我知道rand()生成一个随机数.我找到了这个例子:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
time_t t;
n = 5;
/* Intializes random number generator */
srand((unsigned) time(&t));
/* Print 5 random numbers from 0 to 49 */
for( i = 0 ; i < n ; i++ )
{
printf("%d\n", rand() % 50);
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
我知道这个问题可能听起来有点傻,但任何人都可以解释一下上面的代码是如何工作的吗?