学不会了,大一c语言习题

1、从键盘输入六个整数存入数组,再将数组元素中值为奇数的值写入到a.txt文件中。
2、假设有文件my_file.txt,其中存有30个整数,将其读取出来存入一个数组中。再将其按降序排序后,输出在屏幕上。

你题目的解答代码如下:

img

#include<stdio.h>

int main()
{
    int a[6];
    int len=6,i;
    FILE *fp;
    fp = fopen("a.txt", "w");
    for(i=0;i<len;i++)
       scanf("%d", &a[i]);
    for(i=0;i<len;i++)
        if (a[i]%2==1)
        {
           printf("%d ",a[i]);
           fprintf(fp,"%d ",a[i]);
        }
    fclose(fp);
    return 0;
}

2

#include<stdio.h>

//冒泡排序函数
void sort(int a[],int n)
{
    int i,j;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j] < a[j+1])
            {
                int t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
}

int main()
{
    int a[100];
    int len=0,i,n;
    FILE *fp;
    fp = fopen("my_file.txt", "r");
    while (fscanf(fp,"%d",&n)>0)  //从txt读取一个整数给n
    {
        a[len++] = n;  //把n添加到数组中
    }
    fclose(fp);    //关闭txt
    sort(a,len);   //排序
    for(i=0;i<len;i++)
    {
       printf("%d ", a[i]);   //输出数组的值
    }
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

1
#include<stdio.h>

int main(){
    int array[6];
    int i;
    for(i =0;i<6;i++){
        scanf("%d",array+i);        
    }
    FILE*fp = fopen("a.txt","w");
    for(i=0;i<6;i++){
        if(array[i]%2==1){
            fprintf(fp,"%d\t",array[i]);
        }
    }
    fclose(fp);
    return 0;
}
2

冒泡排序

#include<stdio.h>

void guluguluSort(int array[],int n){
    int temp;
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-1-i;j++){
            if(array[j]<array[j+1]){
                temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}

int main(){
    int array[30] ;
    FILE*fp = fopen("my_file.txt","r");
    for(int i = 0;i<30;i++){
        fscanf(fp,"%d",array+i);
    }
    fclose(fp);
    guluguluSort(array,30);
    for(int i =0;i<30;i++){
        printf("%d\t",array[i]);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int a[6], i;
    FILE*pf = fopen("a.txt", "w");
    printf("请输入六个数:\n");
    for (i = 0; i < 6; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] % 2 == 1)
            fprintf(pf, "%d ", a[i]);
    }
    fclose(pf);
    pf = NULL;
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
int cmp(int e1, int e2)
{
    return e2 - e1;
}

int main()
{
    int a[30], i;
    FILE*pf = fopen("my_file.txt", "r");
    for (i = 0; i < 30; i++)
        fscanf(pf, "%d", &a[i]);
    qsort(a, 30, sizeof(int), cmp);
    for (i = 0; i < 30; i++)
        printf("%d ", a[i]);
    fclose(pf);
    pf = NULL;
    return 0;
}

1、

#include <stdio.h>
void main()
{
    FILE *fp;
    int i, a[6]; //i为数组元素下标
    fp = fopen("a.txt", "w");
    if (fp == NULL)
    {
        printf("Can't open the file!\n");
    }
    printf("请输入6个数:\n");
    for (i = 0; i < 6; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] % 2 == 1)
            fprintf(fp, "%d ", a[i]);
    }
}

2、
1、用put 函数把数组元素放到文件中(ofstream p p.put() 记住一个一个的放)
2 、用 get 函数把数组元素输出到屏幕就可以了