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

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

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

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Q&A

解決済

2回答

1038閲覧

2次元配列がエラーになる

seika

総合スコア6

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

0グッド

0クリップ

投稿2023/04/25 02:02

実現したいこと

2次元配列のエラーを解決する。イメージ説明

前提

エネルギーを節約する換気システムの設計を完成させるために、いくつかの都市で外の平均気温の測定が実施されました。
気候条件と平均気温は毎週測定されます。
コード出力では、挿入されたデータを都市に基づいて週ごとの整理するコードを書きなさい。

というような課題にC++で取り組んでいます。

C言語を始めたばかりで混乱してます、、、
Visual studioを使っています。
オンラインコンパイラも使ってみたのですが、そちらもエラーでした。

発生している問題・エラーメッセージ

添付参照 expected an expression expression must have a constant value expression must have pointer-to-object type but it has double

該当のソースコード

C++

1 2#include <stdio.h> 3int Assignment1() { 4 5 // Define a two-dimensional array to hold the weekly temperature data for all cities 6 int num_cities = 3; 7 int num_weeks = 5; 8 double temperatures = [num_cities][num_weeks]; 9 10 11 // Define an array of city names 12 char cities[num_cities] = { "New York", "Los Angeles", "Chicago" }; 13 14 // Input the weekly temperature data for each city 15 16 for (int i = 0; i < num_cities; i++) { 17 printf("Enter the weekly temperature data for %d:\n", cities[i]); 18 for (int j = 0; j < num_weeks; j++) { 19 scanf("%lf", &temperatures[i][j]); 20 } 21 } 22 23 // Output the weekly temperature data for each city 24 for (int i = 0; i < num_cities; i++) { 25 printf("Weekly temperature data for %s:\n", cities[i].c_str()); 26 for (int j = 0; j < num_weeks; j++) { 27 printf("%.2f ", temperatures[i][j]); 28 } 29 printf("\n"); 30 } 31 return 0; 32}

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

hoshi-takanori

2023/04/25 02:18

Objective-C ではなさそうだし、C と C++ も異なる言語ですけど…。
guest

回答2

0

ベストアンサー

ひとまずコンパイル・エラーを解消してみた。

C++

1#include <cstdio> 2 3int Assignment1() { 4 5 // Define a two-dimensional array to hold the weekly temperature data for all cities 6 const int num_cities = 3; 7 const int num_weeks = 5; 8 double temperatures[num_cities][num_weeks]; 9 10 // Define an array of city names 11 const char* cities[num_cities] = { "New York", "Los Angeles", "Chicago" }; 12 13 // Input the weekly temperature data for each city 14 15 for (int i = 0; i < num_cities; i++) { 16 printf("Enter the weekly temperature data for %s:\n", cities[i]); 17 for (int j = 0; j < num_weeks; j++) { 18 scanf("%lf", &temperatures[i][j]); 19 } 20 } 21 22 // Output the weekly temperature data for each city 23 for (int i = 0; i < num_cities; i++) { 24 printf("Weekly temperature data for %s:\n", cities[i]); 25 for (int j = 0; j < num_weeks; j++) { 26 printf("%.2f ", temperatures[i][j]); 27 } 28 printf("\n"); 29 } 30 return 0; 31} 32 33int main() { 34 Assignment1(); 35}

[追記] C++ に寄せたやつ。

C++

1#include <iostream> 2#include <iomanip> 3#include <array> 4#include <string> 5 6void Assignment1() { 7 8 // Define a two-dimensional array to hold the weekly temperature data for all cities 9 const int num_cities = 3; 10 const int num_weeks = 5; 11 std::array<std::array<double,num_weeks>,num_cities> temperatures; 12 13 // Define an array of city names 14 const std::string cities[num_cities] = { "New York", "Los Angeles", "Chicago" }; 15 16 // Input the weekly temperature data for each city 17 18 for (int i = 0; i < temperatures.size(); ++i ) { 19 std::cout << "Enter the weekly temperature data for " << cities[i] << std::endl; 20 for ( double& temp : temperatures[i] ) { 21 std::cin >> temp; 22 } 23 } 24 25 // Output the weekly temperature data for each city 26 for (int i = 0; i < temperatures.size(); ++i) { 27 std::cout << "Weekly temperature data for " << cities[i] << std::endl; 28 for ( double temp : temperatures[i] ) { 29 std::cout << std::fixed << std::setprecision(2) << temp << ' '; 30 } 31 std::cout << std::endl; 32 } 33} 34 35int main() { 36 Assignment1(); 37}

投稿2023/04/25 03:21

編集2023/04/25 08:12
episteme

総合スコア16614

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

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

0

expression must have a constant valueはメッセージのとおりです。char cities[num_cities]のように宣言する配列は、個数を定数にしなければなりません。

double temperatures = [num_cities][num_weeks];は、そもそも何をしたいのかがわかりません。


せっかくC++なのですから、生の配列など使わず、vectorなどSTLのデータ構造を活用することをおすすめします。

投稿2023/04/25 02:06

maisumakun

総合スコア145121

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

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

seika

2023/04/25 02:12

ユーザーが任意で個数を決める場合はどうしたら良いのでしょうか? とりあえず授業で習ったことの範囲で書いていくことになっているので、配列で書きたいです!
maisumakun

2023/04/25 02:16 編集

「ユーザーが任意で個数を決める」のと「配列」は、ユーザーから来るデータ量に対して余裕を持って配列を取り、不要な部分を余らせるようなムダを出すのでなければ両立できません。 newで動的確保するかSTLを使うかです。
seika

2023/04/25 02:16

そうなのですね それがわかっただけでもありがたいです!! ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問