首页 > 其他分享 >实验4

实验4

时间:2024-11-10 23:01:20浏览次数:1  
标签:return int void ++ 实验 printf define

任务1:

#include<stdio.h>
#define N 4
#define M 2

void test1()
{
int x[N]={1,9,8,4}    ;
int i;
printf("sizeof(x) = %d\n", sizeof(x));


for (i = 0; i < N; ++i)
{
    
printf("%p: %d\n", &x[i], x[i]);

}
printf("x = %p\n", x);
}
void test2()
{
    int x[M][N] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
int i, j;
    
    for (i = 0; i < M; ++i)    
for (j = 0; j < N; ++j)

printf("%p: %d\n", &x[i][j], x[i][j]);
printf("\n");
printf("x = %p\n", x);
printf("x[0] = %p\n", x[0]);
printf("x[1] = %p\n", x[1]);
printf("\n");

}
int main()
{
    
printf("测试1: int型一维数组\n");
test1();
printf("\n测试2: int型二维数组\n");
test2();
    
}

问题1:

一维数组x是连续存放的。x代表它对应的值,&x[0]代表他的地址

问题2:是“按行连续存放的”,x,x[0],&x[0]字面上不一样,差值是十六是因为x的数据的大小是16

 

任务2;

#include <stdio.h>
#define N 100
// 函数声明
void input(int x[], int n) 
{ int i;
for(i = 0; i < n; ++i)
scanf("%d", &x[i]);
}
double compute(int x[], int n) 
{    
int i, high, low;
double ans;
high = low = x[0];
ans = 0;
for(i = 0; i < n; ++i) 
{
ans += x[i];
if(x[i] > high)
high = x[i];
else if(x[i] < low)
low = x[i];
}
ans = (ans - high - low)/(n-2);
return ans;
}
int main() {
int x[N];
int n, i;
double ans;
while(printf("Enter n: "), scanf("%d", &n) != EOF) {
input(x, n); 
ans = compute(x, n); 
printf("ans = %.2f\n\n", ans);
}
return 0;
}

 

问题1:作为形参的时候是x[i],作为实参的时候是&x[i]

问题2:input的作用是将数组中的数一一赋值用于后续使用

compute的作用是去掉一个最大的数和最小的数然后将剩余的数求平均值

 

任务3:

#include <stdio.h>
#define N 100
void output(int x[][N], int n) {
int i, j;
for(i = 0; i < n; ++i) {
for(j = 0; j < n; ++j)
printf("%d ", x[i][j]);
printf("\n");
}
}
void init(int x[][N], int n, int value) 
{
int i, j;
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
x[i][j] = value;
}

int main() {
int x[N][N];
int n, value;
while(printf("Enter n and value: "), scanf("%d%d", &n, &value) != EOF) {
init(x, n, value); 
output(x, n); 
printf("\n");
}
return 0;
}

 

问题1:第二维不可以省略

问题2:output的作用是将一个二维数组输出

init的作用是给这个数组的每一个元素赋一个相同的值

任务4:

#include<stdio.h>
#define N 100

float median(int x[],int n)
{
    float as;
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i+1;j++)
        {
        int temp=0;
        if(x[j]>x[j+1])
        {
            temp=x[j];
            x[j]=x[j+1];
            x[j+1]=temp;
        }    
        }
        if(n%2==0)
        {
            as=(x[n/2]+x[n/2+1])/2;
        }
        else
        {
            as=x[n/2+1];
        }
    }
    
    
return as;    
}
void input(int x[],int n)
{   int i;
    for(i=0;i<n;i++)
    {   int d=0;
        scanf("%d" , &d);
        x[i]=d;
    }
}
void pr(int x[],int n){
    
    for(int i=0;i<n;i++)
    {
         printf("&d",&x[i]);
     }
}
int main()
{
    
    

    int x[N];
    int n;
    double ans;
    
    while(printf("Enter n: "), scanf("%d", &n) != EOF) 
    {
     input(x, n); 
     ans = median(x, n); 
     printf("ans = %g\n\n", ans);
     pr(x,n);
    }
return 0;

}

 任务5:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 100

void input(int x[][N], int n) {
    int i, j;
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
            scanf("%d", &x[i][j]);
    }
}
void output(int x[][N], int n) {
    int i, j;

    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
            printf("%4d", x[i][j]);
        printf("\n");
    }
}
void rotate_to_right(int x[][N], int n)
{
    int c;
    int r;
    int a[N];
    for (r = 0; r < n; r++)
    {
        a[r] = x[r][n - 1];
    }
    for (c = n - 2; c >= 0; c--)
    {
        for (r = 0; r < n; r++)
        {
            x[r][c + 1] = x[r][c];
        }
    }
    for (r = 0; r < n; r++)
    {
        x[r][0] = a[r];
    }
}

int main() {
         int x[N][N];
         int n;
     printf("输入n: ");
     scanf("%d", &n);
     input(x, n);
    printf("原始矩阵:\n");
      output(x, n);
      rotate_to_right(x, n);
    printf("变换后矩阵:\n");
      output(x, n);
        return 0;

}

 任务6:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 100

void dec_to_n(int x, int n)
{
    int res[100];
    int i;
    for (i = 0; x != 0; i++)
    {
        res[i] = x % n;
        x /= n;
    }
    for (i -= 1; i >= 0; i--)
    {
        if (res[i] >= 10)
            printf("%c", res[i] - 10 + 'A');
        else
            printf("%d", res[i]);
    }
    printf("\n");
}

int main(){
            int x;
            while (printf("输入十进制整数: "), scanf("%d", &x) != EOF) {
                dec_to_n(x, 2);
                dec_to_n(x, 8);
                dec_to_n(x, 16);
                printf("\n");
            }
           return 0;
}

 任务7:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 100

void input(int x[][N], int n);
void output(int x[][N], int n);
int is_magic(int x[][N], int n);

int main() {
       int x[N][N];
        int n;
            while (printf("输入n: "), scanf("%d", &n) != EOF) {
                 printf("输入方阵:\n");
                input(x, n);
                printf("输出方阵:\n");
                output(x, n);
        if (is_magic(x, n))
                    printf("是魔方矩阵\n\n");
               else
                   printf("不是魔方矩阵\n\n");
    }
            return 0;
}
void input(int x[][N], int n) {
   int i, j;
       for (i = 0; i < n; ++i) {
                for (j = 0; j < n; ++j)
                    scanf("%d", &x[i][j]);
    }
}
void output(int x[][N], int n) {
    int i, j;
           for (i = 0; i < n; ++i) {
               for (j = 0; j < n; ++j)
                   printf("%4d", x[i][j]);
               printf("\n");
    }
    
}

 int is_magic(int x[][N], int n)
 {
         int i, j;
         int real = 0, test = 0;
        for (i = 0, j = 0; i < n; i++, j++)
            {
                 real += x[i][j];
            }
         for (i = 0, j = n - 1; j >= 0; i++, j--)
           {
            test += x[i][j];
            }
         if (test != real)
             return 0;
        test = 0;
        for (i = 0; i < n; i++)
         {
                for (j = 0; j < n; j++)
                       test += x[i][j];
                 if (test != real)
                         return 0;
                 test = 0;
             }
         for (j = 0; j < n; j++)
            {
                for (i = 0; i < n; i++)
                         test += x[i][j];
                 if (test != real)
                         return 0;
                 test = 0;
            }
        return 1;
    }

 

标签:return,int,void,++,实验,printf,define
From: https://www.cnblogs.com/keobo/p/18524476

相关文章

  • c++实验三
    task1:代码:button.hpp:1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label()......
  • 实验4
    1:代码:#include<stdio.h>#defineN4#defineM2voidtest1(){intx[N]={1,9,8,4};inti;//输出数组x占用的内存字节数printf("sizeof(x)=%d\n",sizeof(x));//输出每个元素的地址、值for(i=0;i<N;++i)printf(......
  • 程序设计实验3
    任务1task1.cpp1#include"window.hpp"2#include<iostream>34usingstd::cout;5usingstd::cin;67voidtest(){8Windoww1("newwindow");9w1.add_button("maximize");10w1.display();11......
  • 实验3
    task1.button.hpp#pragmaonce#include<iostream>#include<string>usingstd::string;usingstd::cout;//按钮类classButton{public:Button(conststring&text);stringget_label()const;voidclick();private:string......
  • 实验3 c++
    任务一:button.hpp:#pragmaonce#include"button.hpp"#include<vector>#include<iostream>usingstd::vector;usingstd::cout;usingstd::endl;//窗口类classWindow{public: Window(conststring&win_title); voiddisplay()const......
  • 实验3 类和对象 基础编程2
    实验任务1:源代码button.hpp:点击查看代码1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label(......
  • 实验3 类和对象_基础编程2
    任务1源程序:button.hpp1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label()cons......
  • 实验3 类和对象_基础编程2
    任务一task1.cppbutton.hpp#pragmaonce#include<iostream>#include<string>usingstd::string;usingstd::cout;//按钮类classButton{public:Button(conststring&text);stringget_label()const;voidclick();private:string......
  • 实验3 类和对象_基础编程2
    实验任务1button.cpp源码#pragmaonce#include<iostream>#include<string>usingstd::string;usingstd::cout;//按钮类classButton{public:Button(conststring&text);stringget_label()const;voidclick();private:string......
  • 产品手册工具在实验室仪器行业的应用
    大家好,这里是ai元启航,最近在学习ai知识,今天分享的是有关产品手册工具在实验室仪器行业的应用,我们知道,实验室仪器行业作为高新技术领域的重要组成部分,其产品手册的详尽程度与易用性对于用户的使用体验与满意度具有重要影响。为了提升产品手册的实用性与便捷性,越来越多的实验室仪器......