質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

1回答

556閲覧

Word values

Merrifield

総合スコア31

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2020/09/05 13:21

編集2020/09/06 06:09

codewarsというサイト内で問題を解いています。https://www.codewars.com/kata/598d91785d4ce3ec4f000018/train/c
文字列を与えられ、アルファベットのそれぞれの文字が値を持つとして、(a = 1, b = 2, c = 3 ....z = 26)

["abc","abc abc"] → [6,24]  

上の例 : 一つ目の要素"abc" →a(=1)+b(=2)+c(=3) ×1
二つ目の要素"abc abc" → { (1+2+3)+ (1+2+3) } ×2

このように、それぞれの文字が持つ値の和と要素の位置の掛け算を値として返す関数を作成しています。下のようにコードを作ったのですが、問題点があれば教えていただけないでしょうか?また、コードのより良い書き方や、不要な部分を教えていただければ嬉しいです。

c

1#include <stddef.h> 2#include <stdlib.h> //malloc 3#include <string.h> //strlen 4#include <ctype.h> //isalpha 5 6const int* name_value(size_t n, const char *const words[n]) { 7 8 size_t i; 9 const int* answer = malloc(sizeof(int) * n ); 10 11 12 for(i = 0; i < m; i++) 13 { 14 int l = strlen(words[i]); 15 for(size_t j = 0; j < (size_t)l; j++) 16 { 17 18 if (!isalpha((int)words[i][j]))continue; //空白だったらスキップ 19 20 answer[i] = answer[i] + (words[i][j] - 96); //文字を数値に変換 21 22 23 } 24 25 answer[i] = answer[i] * (i + 1); 26 27 28 } 29 30 31 return answer; 32}

エラー

fixture.c:10:5: warning: implicit declaration of function 'tester' is invalid in C99 [-Wimplicit-function-declaration] tester(4, words, expected); ^ fixture.c:15:5: warning: implicit declaration of function 'tester' is invalid in C99 [-Wimplicit-function-declaration] tester(3, words, expected); ^ 2 warnings generated. solution.c:12:20: error: use of undeclared identifier 'm' for(i = 0; i < m; i++) ^ solution.c:20:19: error: read-only variable is not assignable answer[i] = answer[i] + (words[i][j] - 96); ~~~~~~~~~ ^ solution.c:25:17: error: read-only variable is not assignable answer[i] = answer[i] * (i + 1); ~~~~~~~~~ ^ 3 errors generated. setup.c:11:17: warning: format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat] i, expected[i], submitted[i]); ^ /usr/include/criterion/internal/assert.h:529:62: note: expanded from macro 'cr_assert' #define cr_assert(...) CR_EXPAND(cr_assert_(__VA_ARGS__)) ^~~~~~~~~~~ /usr/include/criterion/internal/assert.h:159:28: note: expanded from macro 'cr_assert_' CR_VA_TAIL(__VA_ARGS__) \ ^~~~~~~~~~~ /usr/include/criterion/internal/preprocess.h:41:103: note: expanded from macro 'CR_VA_TAIL' #define CR_VA_TAIL(...) CR_EXPAND(CR_VA_TAIL_HELPER(CR_VA_TAIL_SELECT(__VA_ARGS__), __VA_ARGS__)) ^~~~~~~~~~~ note: (skipping 30 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all) /usr/include/criterion/internal/preprocess.h:35:43: note: expanded from macro 'CR_EXPAND' #define CR_EXPAND(x) x ^ /usr/include/criterion/internal/preprocess.h:35:43: note: expanded from macro 'CR_EXPAND' #define CR_EXPAND(x) x ^ /usr/include/criterion/internal/preprocess.h:35:43: note: expanded from macro 'CR_EXPAND' #define CR_EXPAND(x) x ^ 1 warning generated.

テストコード

c

1#include <criterion/criterion.h> 2#include <stddef.h> 3 4int* name_value(size_t n, const char *const words[n]); 5 6Test(Example_Tests, should_pass_these_two_tests) { 7 { 8 const char *const words[4] = {"abc", "abc", "abc", "abc"}; 9 const int expected[4] = {6, 12, 18, 24}; 10 tester(4, words, expected); 11 } 12 { 13 const char *const words[3] = {"codewars", "abc", "xyz"}; 14 const int expected[3] = {88, 12, 225}; 15 tester(3, words, expected); 16 } 17}

修正版

c

1#include <stddef.h> 2#include <stdlib.h> //malloc 3#include <string.h> //strlen 4#include <ctype.h> //isalpha 5 6const int* name_value(size_t n, const char *const words[n]) { 7 8 size_t i; 9 int* answer = calloc(n,sizeof(int) ); 10 11 12 for(i = 0; i < n; i++) 13 { 14 int l = strlen(words[i]); 15 for(size_t j = 0; j < (size_t)l; j++) 16 { 17 18 if (!isalpha(words[i][j]))continue; //空白だったらスキップ 19 20 answer[i] += (words[i][j] - 'a' - 1); //文字を数値に変換 21 22 23 } 24 25 answer[i] *= (i + 1); 26 27 28 } 29 30 31 return answer; 32}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kazuma-s

2020/09/06 03:18

エラーメッセージのうち、warning よりもまず error を見てください。 自分の書いたコードのエラーが表示されています。
guest

回答1

0

ベストアンサー

const int* answer = だと answer[i] が書き込み禁止になります。
初期化すらできません。というか 0 に初期化せずに和を計算していますね。
なぜ const を付けたんですか?

for(i = 0; i < m; i++)m はどこにも宣言されていません。
n の間違いでしょう。

isalpha((int)words[i][j])(int) は不要。

words[i][j] - 9696 が分かりにくい。('a' - 1) のほうが良い。

answer[i] = answer[i] +answer[i] += と書けます。
answer[i] = answer[i] *answer[i] *= と書けます。

投稿2020/09/05 18:16

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Merrifield

2020/09/06 01:05 編集

>>なぜ const を付けたんですか? 戻り値がconstだったので、付けたのですが必要なのでしょうか?あと、初期化は必要でしたね。 >>for(i = 0; i < m; i++) の m はどこにも宣言されていません。 n の間違いでしょう。 これはそうですね。 >>isalpha((int)words[i][j]) の (int) は不要 (int)は必要なかったのですね。 >>answer[i] = answer[i] + は answer[i] += と書けます。 answer[i] = answer[i] * は answer[i] *= と書けます。 その書き方で良いのですね。ありがとうございます。
Merrifield

2020/09/06 00:59

>>words[i][j] - 96 の 96 が分かりにくい。('a' - 1) のほうが良い。 これはwords[i][j] - ('a' - 1)と書いたほうが良いということでしょうか?
kazuma-s

2020/09/06 03:16

word[i][j] が 'a' のとき、式の値が 1 になることが明白ですよね。 'a' - 96 と書いたら、それが 1 になることはどうやって分かるんですか? ところで、全体の修正の結果はどうなったんですか?
Merrifield

2020/09/06 06:20 編集

そういうものなのですね。 修正結果は、質問文に載せました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問