c - 改进 MIPS 汇编语言的以下代码
问题描述
我的程序的目的是根据用户输入对数字进行排序。一切正常,直到用户想要输入超过 6 个数据(inp = 5),我不知道为什么会这样。你能回顾一下并告诉我背后的原因吗?
#include <stdio.h>
#include <stdlib.h>
int size =0;
void ascSort(int *a, int size, int temp) {
for(int i=1; i< size; i++){
for(int j=i; j>0; j--){
if(a[j-1]>a[j]){
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
void descSort(int *a, int size, int temp) {
for(int i=1; i< size; i++){
for(int j=i; j>0; j--){
if(a[j-1]<a[j]){
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
int main(){
char inpChar, isContinue='y';
int inp, temp=0;
int * a = malloc(0 * sizeof(*a));
do {
printf("How many numbers would you like to sort?\n");
fflush(stdin);
scanf("%d", &inp);
size = inp;
printf("%d\n", size);
a = realloc(a, size * sizeof(*a));
for(int i=0; i<size; i++){
//Below, I am printing 'size' for debugging purposes
printf("Type %d more numbers to sort ---- '%d'\n", inp, size);
fflush(stdin);
scanf("%d", &a[i]);
inp--;
}
printf("Please input a for ascending order and d for descending order\n");
fflush(stdin);
scanf(" %c", &inpChar);
switch (inpChar) {
case 'a':
ascSort(a, size, temp);
break;
case 'd':
descSort(a, size, temp);
break;
default: printf("Invalid character\n");
}
printf("Sorted Array: \n" );
for(int i=0; i<size; i++){
printf("%d\n", a[i]);
}
printf("Do you want to repeat? Type y/n\n");
fflush(stdin);
scanf(" %c", &isContinue);
} while(isContinue != 'n');
}
在“inp”值增加到 6 后,“inp”值显示出完全不符合预期的垃圾值。
注意代码已更新并且现在可以使用。现在,我的问题是如何改进这段代码,以便我可以将这段代码转换为 MIPS 汇编语言?谢谢。
解决方案
推荐阅读
- heroku - heroku 日志中的 heroku favicon 错误 h 10
- javascript - 我可以在 JS 的控制台中获取一些字符串并将其导入 html 吗?
- html - 有没有办法用 css/html 在页面上的每个链接上附加一些东西?
- javascript - React Native Firebase 苹果身份验证错误:[auth/invalid-credential] 提供的身份验证凭据格式错误或已过期
- r - 导入自制数据框后出现问题
- c - 在 C 中打印 n 叉树
- oracle - 如何在 oracle apex 中导入导出现有应用程序
- angular - Angular/Asp.net core 3.1 web.config 问题
- angular - 不允许使用 Bottle 服务器 CORS 处理 Angular HTTP 请求
- python - Python 请求和实时会话