问:数组和函数作业
Q: Arrays and Functions Homework
坦率地说,就目前而言,我目前不太擅长编码。我真的很想完成这些功能,我只是在执行其中一些时遇到了问题。
基本上,有 10 个函数(其中 6 个我已经完成?)需要创建。
有一个 int main() 但是,除了需要修复的字符串之外,不需要触及 int main() 。
我将 post 除了 int main() 之外的程序(主要是为了让您可以看到我的工作),这样如果有人想检查意图,他们就可以看到它。尽管我已经 post 编辑了整个程序,但我真正想关注的唯一一个是 makeUpper,以及其中没有任何内容的函数,因为我真的不明白如何复制和编辑字符串进入另一个数组。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void makeUpper( char orig[], char result[] )
{
/*
int i = 0;
while ( str[i] != '[=10=]' )
{
if ( str[i] >= 'a' && str[i] <= 'z' )
str[i] = str[i] - 32;
k++;
}
*/
}
int countDigit( char str[] )
{
int i;
int count = 0;
int len;
len = strlen(str);
for ( i = 0; i < len; i++ )
{
if ( i >= '0' && i <= '9' )
count++;
}
return count;
}
int onlyLetters( char orig[], char letters[] )
{
}
int countOdd( int A[], int N )
{
int i;
int odd = 0;
for ( i = 0; i < N; i++ );
{
if ( A[i] % 2 != 0 )
odd++;
}
return odd;
}
int sumList( int A[], int N )
{
int i;
int sum = 0;
for ( i = 0; i < N; i++ );
sum = sum + A[i];
return sum;
}
void addToEach( int A[], int N, int val )
{
int i;
for ( i = 0; i < N; i++ )
A[i] + val;
printf("%d", A[i]);
}
void copyNumList( int orig[], int N, int result[] )
{
}
// populate list A with N items, random in range L to H
void fillNumList( int A[], int N, int L, int H )
{
}
// print the list, 10 items per line
void printList( int A[], int N )
{
int i;
for ( i = 0; i < N; i++ )
printf("%d ", A[i]);
printf("\n");
}
void rept( int n, char c )
{
int i;
for ( i = 0; i < n; i++ )
printf("%c", c);
printf("\n");
}
int main()
{
char line[100], other[100];
int i, len;
printf("Phrase: ");
fgets( line, 100, stdin );
// fix the string....
// ...
// really, do this
// ...
rept(10,'-');
printf("Orig: @@%s@@\n", line);
rept(10,'-');
makeUpper( line, other );
printf("toupper: %s\n", other);
rept(10,'-');
i = countDigit( line );
printf("%d digits\n", i);
rept(10,'-');
len = onlyLetters( line, other );
printf("only letters: %s\n", other );
printf(" new len %d\n", len);
int nums[30], vals[30];
int many = 19;
rept(5, '-');
fillNumList( nums, many, 3, 11 );
printList( nums, many );
rept(5, '-');
i = countOdd( nums, many );
printf("%d odd values\n", i);
rept(5, '-');
i = sumList( nums, many );
printf("%d is sum\n", i);
rept(5, '-');
copyNumList( nums, many, vals );
printf("Copy\n");
printList( vals, many );
rept(5, '-');
addToEach( vals, many, 4 );
printf("Add 4\n");
printList( vals, many );
rept(5, '-');
printf("Orig\n");
printList( nums, many );
rept(5, '-');
return 0;
}
回答一个几乎定义明确的问题(关于 makeUpper
):
首先,str
不是变量。您有 orig
和 result
,但尚未在任何地方声明 str
。
现在,如果 str
是 orig
,您将正确地将原始字符串中的字符大写。然而,任务是保留原始字符串并制作一个大写的副本。前者称为"destructive operation"(因为没有保留原来的内容);后者是 "non-destructive operation"(原因很明显)。以下是使您的函数非破坏性所需的更改:
void makeUpper( char orig[], char result[] )
{
int k = 0;
do
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
} while ( orig[k++] != '[=10=]' );
}
请注意,我在 orig
上进行迭代和测试,但分配给 result
。
onlyLetters
应该差不多;但是您将需要两个索引变量,而不仅仅是 k
,因为您将希望以不同的速度在 orig
和 result
之间前进(您只想复制一个字符,当它是字母,当你复制时你只想通过 result
前进——而不是 orig
,你每次循环总是消耗一个字符)。
由于你没有说出你对copyNumList
和fillNumList
的误解是什么,我无法给你任何帮助。
编辑:我忘了终结器(也没有测试);感谢汤姆 Karzes!因为还需要复制零,所以您的循环从 while (condition) { ... }
更改为 do { ... } while (condition)
- 而不是测试我们是否结束然后在没有结束时执行操作,我们将执行先做事,然后才问我们是否完成了。这也需要在测试后完成增量,所以 k++
被移动了。如果这对您来说太奇怪了,您可以通过在循环完成后显式添加终止符来获得与此相同的效果:
void makeUpper( char orig[], char result[] )
{
int k = 0;
while ( orig[k] != '[=11=]' )
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
k++;
}
result[k] = '[=11=]';
}
坦率地说,就目前而言,我目前不太擅长编码。我真的很想完成这些功能,我只是在执行其中一些时遇到了问题。
基本上,有 10 个函数(其中 6 个我已经完成?)需要创建。 有一个 int main() 但是,除了需要修复的字符串之外,不需要触及 int main() 。
我将 post 除了 int main() 之外的程序(主要是为了让您可以看到我的工作),这样如果有人想检查意图,他们就可以看到它。尽管我已经 post 编辑了整个程序,但我真正想关注的唯一一个是 makeUpper,以及其中没有任何内容的函数,因为我真的不明白如何复制和编辑字符串进入另一个数组。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void makeUpper( char orig[], char result[] )
{
/*
int i = 0;
while ( str[i] != '[=10=]' )
{
if ( str[i] >= 'a' && str[i] <= 'z' )
str[i] = str[i] - 32;
k++;
}
*/
}
int countDigit( char str[] )
{
int i;
int count = 0;
int len;
len = strlen(str);
for ( i = 0; i < len; i++ )
{
if ( i >= '0' && i <= '9' )
count++;
}
return count;
}
int onlyLetters( char orig[], char letters[] )
{
}
int countOdd( int A[], int N )
{
int i;
int odd = 0;
for ( i = 0; i < N; i++ );
{
if ( A[i] % 2 != 0 )
odd++;
}
return odd;
}
int sumList( int A[], int N )
{
int i;
int sum = 0;
for ( i = 0; i < N; i++ );
sum = sum + A[i];
return sum;
}
void addToEach( int A[], int N, int val )
{
int i;
for ( i = 0; i < N; i++ )
A[i] + val;
printf("%d", A[i]);
}
void copyNumList( int orig[], int N, int result[] )
{
}
// populate list A with N items, random in range L to H
void fillNumList( int A[], int N, int L, int H )
{
}
// print the list, 10 items per line
void printList( int A[], int N )
{
int i;
for ( i = 0; i < N; i++ )
printf("%d ", A[i]);
printf("\n");
}
void rept( int n, char c )
{
int i;
for ( i = 0; i < n; i++ )
printf("%c", c);
printf("\n");
}
int main()
{
char line[100], other[100];
int i, len;
printf("Phrase: ");
fgets( line, 100, stdin );
// fix the string....
// ...
// really, do this
// ...
rept(10,'-');
printf("Orig: @@%s@@\n", line);
rept(10,'-');
makeUpper( line, other );
printf("toupper: %s\n", other);
rept(10,'-');
i = countDigit( line );
printf("%d digits\n", i);
rept(10,'-');
len = onlyLetters( line, other );
printf("only letters: %s\n", other );
printf(" new len %d\n", len);
int nums[30], vals[30];
int many = 19;
rept(5, '-');
fillNumList( nums, many, 3, 11 );
printList( nums, many );
rept(5, '-');
i = countOdd( nums, many );
printf("%d odd values\n", i);
rept(5, '-');
i = sumList( nums, many );
printf("%d is sum\n", i);
rept(5, '-');
copyNumList( nums, many, vals );
printf("Copy\n");
printList( vals, many );
rept(5, '-');
addToEach( vals, many, 4 );
printf("Add 4\n");
printList( vals, many );
rept(5, '-');
printf("Orig\n");
printList( nums, many );
rept(5, '-');
return 0;
}
回答一个几乎定义明确的问题(关于 makeUpper
):
首先,str
不是变量。您有 orig
和 result
,但尚未在任何地方声明 str
。
现在,如果 str
是 orig
,您将正确地将原始字符串中的字符大写。然而,任务是保留原始字符串并制作一个大写的副本。前者称为"destructive operation"(因为没有保留原来的内容);后者是 "non-destructive operation"(原因很明显)。以下是使您的函数非破坏性所需的更改:
void makeUpper( char orig[], char result[] )
{
int k = 0;
do
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
} while ( orig[k++] != '[=10=]' );
}
请注意,我在 orig
上进行迭代和测试,但分配给 result
。
onlyLetters
应该差不多;但是您将需要两个索引变量,而不仅仅是 k
,因为您将希望以不同的速度在 orig
和 result
之间前进(您只想复制一个字符,当它是字母,当你复制时你只想通过 result
前进——而不是 orig
,你每次循环总是消耗一个字符)。
由于你没有说出你对copyNumList
和fillNumList
的误解是什么,我无法给你任何帮助。
编辑:我忘了终结器(也没有测试);感谢汤姆 Karzes!因为还需要复制零,所以您的循环从 while (condition) { ... }
更改为 do { ... } while (condition)
- 而不是测试我们是否结束然后在没有结束时执行操作,我们将执行先做事,然后才问我们是否完成了。这也需要在测试后完成增量,所以 k++
被移动了。如果这对您来说太奇怪了,您可以通过在循环完成后显式添加终止符来获得与此相同的效果:
void makeUpper( char orig[], char result[] )
{
int k = 0;
while ( orig[k] != '[=11=]' )
{
if ( orig[k] >= 'a' && orig[k] <= 'z' )
result[k] = orig[k] - 32;
else
result[k] = orig[k];
k++;
}
result[k] = '[=11=]';
}