開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅡ部(シンプルなプログラミング)の9章(変数のスコープと関数)、9.8(プログラミング実習)、実習 9-5.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 9-5.
コード(BBEdit, Emacs)
sample.cpp
#include <iostream>
/* max -- 数値配列の最大値を返す関数
*
* 引数
* nums -- 数値配列
* length - 配列の長さ
*
* 戻り値
* 最大値
*/
int max(int nums[], int length)
{
int n;
int i;
n = nums[0];
for (i = 1; i < length; ++i) {
if (nums[i] > n) {
n = nums[i];
}
}
return (n);
}
int main()
{
int nums1[] = {5, 1, 4, 2, 3};
int nums2[] = {5, 1, 4, 2, 3, 5, 1, 4, 2, 3};
std::cout << max(nums1, 5) << '\n';
std::cout << max(nums2, 10) << '\n';
return (0);
}
Makefile
# # FSFのg++コンパイラ用のMakefile # CC=g++ CFLAGS=-g -Wall all: sample sample: sample.cpp $(CC) $(CFLAGS) -o sample sample.cpp clean: rm sample
入出力結果(Terminal)
$ make && ./sample g++ -g -Wall -o sample sample.cpp 5 5 $
0 コメント:
コメントを投稿