Skip to content

Instantly share code, notes, and snippets.

@goodjack
Created November 13, 2018 12:34
Show Gist options
  • Select an option

  • Save goodjack/e0bc4d3927bd04c0a6a0cf5c802ae029 to your computer and use it in GitHub Desktop.

Select an option

Save goodjack/e0bc4d3927bd04c0a6a0cf5c802ae029 to your computer and use it in GitHub Desktop.
[CS50 2018] [Lecture 2] Arrays (C++ version)
#include <iostream>
using namespace std;
int main(void) {
// 取得使用者的分數
int score1, score2, score3;
cout << "Score 1:";
cin >> score1;
cout << "Score 2:";
cin >> score2;
cout << "Score 3:";
cin >> score3;
// 產生第一條
cout << "Score 1: ";
for (int i = 0; i < score1; i++) {
cout << "#";
}
cout << "\n";
// 產生第二條
cout << "Score 2: ";
for (int i = 0; i < score2; i++) {
cout << "#";
}
cout << "\n";
// 產生第三條
cout << "Score 3: ";
for (int i = 0; i < score3; i++) {
cout << "#";
}
cout << "\n";
}
#include <iostream>
using namespace std;
void chart(int score);
int main(void) {
// 取得使用者的分數
int score1, score2, score3;
cout << "Score 1:";
cin >> score1;
cout << "Score 2:";
cin >> score2;
cout << "Score 3:";
cin >> score3;
// 產生第一條
cout << "Score 1: ";
chart(score1);
// 產生第二條
cout << "Score 2: ";
chart(score2);
// 產生第三條
cout << "Score 3: ";
chart(score3);
}
// 產生圖表
void chart(int score) {
// 每一分產生一個井號
for (int i = 0; i < score; i++) {
cout << "#";
}
cout << "\n";
}
// 使用陣列(Array)產生三個分數的長條圖
#include <iostream>
using namespace std;
void chart(int score);
int main(void) {
// 取得使用者的分數
int scores[3];
for (int i = 0; i < 3; i++) {
cout << "Score " << i + 1 << ": ";
cin >> scores[i];
}
// 把分數轉成圖表
for (int i = 0; i < 3; i++) {
cout << "Score " << i + 1 << ": ";
chart(scores[i]);
}
}
// 產生圖表
void chart(int score) {
// 每一分產生一個井號
for (int i = 0; i < score; i++) {
cout << "#";
}
cout << "\n";
}
// 使用陣列(Array)產生三個分數的長條圖
#include <iostream>
using namespace std;
const int COUNT = 3;
void chart(int score);
int main(void) {
// 取得使用者的分數
int scores[COUNT];
for (int i = 0; i < COUNT; i++)
{
cout << "Score " << i + 1 << ": ";
cin >> scores[i];
}
// 把分數轉成圖表
for (int i = 0; i < COUNT; i++)
{
cout << "Score " << i + 1 << ": ";
chart(scores[i]);
}
}
// 產生圖表
void chart(int score) {
// 每一分產生一個井號
for (int i = 0; i < score; i++) {
cout << "#";
}
cout << "\n";
}
// 使用陣列(Array)產生三個分數的長條圖
#include <iostream>
using namespace std;
const int COUNT = 3;
void chart(int count, int scores[]);
int main(void) {
// 取得使用者的分數
int scores[COUNT];
for (int i = 0; i < COUNT; i++)
{
cout << "Score " << i + 1 << ": ";
cin >> scores[i];
}
// 把分數轉成圖表
chart(COUNT, scores);
}
// 產生圖表
void chart(int count, int scores[]) {
// 每一分產生一個井號
for (int i = 0; i < count; i++) {
for (int j = 0; j < scores[i]; j++) {
cout << "#";
}
cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment