Created
February 18, 2019 12:00
-
-
Save KeitetsuWorks/406375298c60732d08f76e0c4b2a604c to your computer and use it in GitHub Desktop.
サンプルプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @file q14203689699.c | |
| * @brief サンプルプログラム | |
| * @author Keitetsu | |
| * @date 2019/02/17 | |
| * @copyright Copyright (c) 2019 Keitetsu | |
| * @par License | |
| * This software is released under the MIT License. | |
| */ | |
| #include <stdio.h> | |
| #define DATA_NUM (10) /**< 整数型配列の大きさ */ | |
| static void print_data(int *data, int data_num); | |
| int main(void) | |
| { | |
| /* | |
| * 大きさ10の整数型配列を用意し、下図のように初期設定しなさい。 | |
| * この配列を順に調べ、奇数の値のみ、別の大きさ10の整数型配列に代入しなさい。 | |
| * また、代入した配列の中身と、何個格納したかを画面表示しなさい。 | |
| * 左にdata1の配列があり、10, 15, 22, 45, 9, 66, 71, 4, 37, 82が入っていて、奇数だけ右のdata2に格納されます。 | |
| */ | |
| int data1[DATA_NUM] = { | |
| 10, 15, 22, 45, 9, 66, 71, 4, 37, 82 | |
| }; | |
| int data2[DATA_NUM] = {0}; | |
| int i; | |
| int odd_number_cnt; | |
| printf("data1[%d]配列\n", DATA_NUM); | |
| print_data(data1, DATA_NUM); | |
| odd_number_cnt = 0; | |
| for (i = 0; i < DATA_NUM; i++) { | |
| if (data1[i] % 2 != 0) { /* 2で割り切れない場合は奇数 */ | |
| data2[odd_number_cnt] = data1[i]; | |
| odd_number_cnt++; | |
| } | |
| } | |
| printf("data2[%d]配列\n", DATA_NUM); | |
| print_data(data2, odd_number_cnt); | |
| printf("格納奇数: %d\n", odd_number_cnt); | |
| return 0; | |
| } | |
| static void print_data(int *data, int data_num) | |
| { | |
| int i; | |
| for (i = 0; i < data_num; i++) { | |
| printf("data[%d] = %d\n", i, data[i]); | |
| } | |
| return; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment