- 2024高階語言程式設計:https://edu.cnblogs.com/campus/fzu/2024C
- 高階語言程式設計課程第五次作業:https://edu.cnblogs.com/campus/fzu/2024C/homework/13303
- 學號:102400215
- 姓名:胡加乘
1
2
#include <iostream>
#include <cmath>
using namespace std;
/*
baseStr: 原來的字串
n: 原來字串的長度
m: 從第m個字元開始複製
replacement: 複製的字串
reLen: 複製的字串的總長度
*/
void replaceStr(char* baseStr, int n, int m, char* replacement, int reLen);
int main()
{
char baseStr[] = "This is a cpp program.";
char replacement[] = "margorp=====";
replaceStr(baseStr, strlen(baseStr), 14,
replacement, strlen(replacement));
cout << baseStr << endl;
return 0;
}
void replaceStr(char* baseStr, int n, int m, char* replacement, int reLen)
{
for (int i = m; i < n; i++)
{
if (i - m < reLen)
baseStr[i] = replacement[i - m];
}
}
3
#include <iostream>
#include <cmath>
using namespace std;
void bubbleSort(int* nums, int len);
int main()
{
int nums[] = { 3, 1, 7, 34, 23, 9, 6, -10, 2, 6, -666 };
int len = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < len; i++)
cout << nums[i] << " ";
cout << endl;
cout << "Here goes the bubble sort!" << endl;
bubbleSort(nums, len);
for (int i = 0; i < len; i++)
cout << nums[i] << " ";
cout << endl;
return 0;
}
void bubbleSort(int* nums, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
4
#include <iostream>
#include <cmath>
using namespace std;
// 3 4 2 7 67 -102 45
// 45 -102 67 7 2 4 3
void reverse(int* nums, int len);
int main()
{
int nums[100];
int length = 0;
while (scanf_s("%d", &nums[length++])); // 讀到字母結束,回車不算!
length--; // 多加一個,減回來
reverse(nums, length);
for (int i = 0; i < length; i++)
{
cout << nums[i] << " ";
}
return 0;
}
void reverse(int* nums, int len)
{
for (int i = 0; i < len / 2; i++)
{
int tmp = nums[i];
nums[i] = nums[len - 1 - i];
nums[len - 1 - i] = tmp;
}
}
5
#include <iostream>
#include <cmath>
using namespace std;
void printArray(int* nums, int len);
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6 };
int len = sizeof(array) / sizeof(array[0]);
printArray(array, len);
return 0;
}
void printArray(int* nums, int len)
{
for (int i = 0; i < len; i++)
{
cout << *(nums + i) << " ";
}
}
6
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
typedef struct
{
int value;
int r, c;
} memberInfo;
void iterateArray(int** arr, int rows, int cols, memberInfo* min, memberInfo* max);
int** generate2dArray(int* rows, int* cols, int maxRows = 10, int maxCols = 10);
void print2dArray(int** array, int rows, int cols);
void clean2dArray(int** array, int rows);
int main()
{
int rows,
cols;
int** array = generate2dArray(&rows, &cols);
print2dArray(array, rows, cols);
cout << endl;
memberInfo min = { INT_MAX, -1, -1 };
memberInfo max = { INT_MIN, -1, -1 };
iterateArray(array, rows, cols, &min, &max);
printf("min value: %d, row=%d, col=%d\n", min.value, min.r, min.c);
printf("max value: %d, row=%d, col=%d\n", max.value, max.r, max.c);
clean2dArray(array, rows);
return 0;
}
int** generate2dArray(int* rows, int* cols, int maxRows, int maxCols)
{
*rows = rand() % maxRows + 1;
*cols = rand() % maxCols + 1;
int** arr = (int**)malloc(sizeof(int*) * *rows);
for (int i = 0; i < *rows; i++)
{
arr[i] = (int*)malloc(sizeof(int) * *cols);
for (int j = 0; j < *cols; j++)
{
arr[i][j] = rand();
}
}
return arr;
}
void print2dArray(int** array, int rows, int cols)
{
cout << "(" << rows << "x" << cols << ")" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void clean2dArray(int** array, int rows)
{
for (int i = 0; i < rows; i++)
free(array[i]);
free(array);
}
void iterateArray(int** arr, int rows, int cols, memberInfo* min, memberInfo* max)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] < min->value)
{
min->value = arr[i][j];
min->r = i;
min->c = j;
}
else if (arr[i][j] > max->value)
{
max->value = arr[i][j];
max->r = i;
max->c = j;
}
}
}
}
7