📌  相关文章
📜  AKTU第一学年第二学期解题纸2017-18 | COMP。系统和C编程| C段

📅  最后修改于: 2021-05-20 07:48:08             🧑  作者: Mango

论文下载链接:论文|第二学期| 2017-18

时间: 3小时
总分:70

注意:-

  • 共分为三个部分。 A节为20分, B节为30分, C节为50分。
  • 尝试所有问题。每个问题都带有标记。
  • 必要时假定合适的数据。

3.尝试以下任何一项:(7 * 1 = 7)

  1. 描述编译器,解释器,汇编器?编写在C编程中使用的编译器的名称。
    语言处理器可以是以下三种类型中的任何一种:
    1. 编译器–
      一口气读取全部以高级语言编写的完整源程序并将其翻译成机器语言的等效程序的语言处理器称为编译器。
      示例: C,C++,C#, Java

      在编译器中,如果没有错误,则源代码将成功转换为目标代码。当源代码中有任何错误时,编译器会在编译结束时用行号指定错误。必须清除这些错误,然后编译器才能再次成功重新编译源代码。

    2. 汇编器–
      汇编程序用于将以汇编语言编写的程序转换为机器代码。源程序是包含汇编语言指令的汇编程序的输入。汇编器生成的输出是计算机可以理解的目标代码或机器代码。

    3. 口译员–
      将源程序的单条语句转换为机器代码是由语言处理器完成的,并在继续进行下一行之前立即执行它,这称为解释器。如果语句中有错误,解释器将在该语句中终止其翻译过程并显示错误消息。解释器仅在消除错误后才继续执行下一行。解释器直接执行以编程或脚本语言编写的指令,而无需事先将其转换为目标代码或机器代码。
      示例: Perl, Python和Matlab。

    C语言编译器的名称:

    • Microsoft Visual Studio社区
    • Xcode
    • Tiny C编译器(TCC)
    • GNU C编译器
  2. 转换以下内容:
    1. (0110110.1100) 2 =() 8 (0110110.1100) 2 =(0110110.110 0) 2 =(66.6) 8
    2. (74.67) 10 =() 16 (74.67) 10 =(4A.AB851EB851EB851EB852) 16
    3. (AB.CD) 16 =() 8 (AB.CD) 16 =(253.632) 8
    4. (EFE.45) 16 =() 2 (EFE.45) 16 =(111011111110.01000101) 2
    5. (576.4) 10 =() 6 (576.4) 10 =(2400.22222222222222222222222) 6
    6. (1234.7) 8 =() 16 (1234.7) 8 =(29C.E) 16
    7. (334.43) 8 =() 2 (334.43) 8 =(11011100.100011) 2

4.尝试以下任何一项:(7 x 1 = 7)

  1. 用示例说明C中可用的不同按位运算运算符。在C语言中,以下6个运算符是按位运算运算符(按位工作)

    &(按位与)将两个数字作为操作数,并对两个数字的每一位进行“与”运算。仅当两个位均为1时,AND的结果才为1。

    | (按位或)将两个数字作为操作数,并对两个数字的每一位进行“或”运算。 OR的结果为1,两个位中的任何一位为1。

    ^(按位XOR)将两个数字用作操作数,并对两个数字的每一位进行XOR。如果两个位不同,则XOR的结果为1。

    <<(左移)取两个数字,左移第一个操作数的位,第二个操作数确定要移位的位数。

    >>(右移)取两个数字,右移第一个操作数的位,第二个操作数确定要移位的位数。

    〜(按位非)取一个数字并将其所有位求反

    以下是示例C程序。

    // C Program to demonstrate
    // use of bitwise operators
      
    #include 
    int main()
    {
      
        unsigned char a = 5, b = 9;
      
        // a = 5(00000101), b = 9(00001001)
        printf("a = %d, b = %d\n", a, b);
      
        // The result is 00000001
        printf("a&b = %d\n", a & b);
      
        // The result is 00001101
        printf("a|b = %d\n", a | b);
      
        // The result is 00001100
        printf("a^b = %d\n", a ^ b);
      
        // The result is 11111010
        printf("~a = %d\n", a = ~a);
      
        // The result is 00010010
        printf("b<<1 = %d\n", b << 1);
      
        // The result is 00000100
        printf("b>>1 = %d\n", b >> 1);
      
        return 0;
    }
    
    输出:
    a = 5, b = 9
    a&b = 1
    a|b = 13
    a^b = 12
    ~a = 250
    b<<1 = 18
    b>>1 = 4
    
  2. 类型转换是什么意思?为什么有必要?用示例说明隐式和显式类型转换。类型转换基本上是从一种类型到另一种类型的转换。类型转换有两种类型:
    1. 隐式类型转换

      也称为“自动类型转换”。

      • 由编译器自行完成,而无需用户的任何外部触发。
      • 通常在表达式中存在多个数据类型时发生。在这种情况下,进行类型转换(类型提升)以避免数据丢失。
      • 变量的所有数据类型都将升级为具有最大数据类型的变量的数据类型。
        bool -> char -> short int -> int -> 
               unsigned int -> long -> unsigned -> 
               long long -> float -> double -> long double
        
      • 隐式转换可能会丢失信息,符号可能会丢失(将符号隐式转换为无符号),并且会发生溢出(当long long被隐式转换为float时)。

      类型隐式转换的示例:

      // An example of implicit conversion
      #include 
      int main()
      {
          int x = 10; // integer x
          char y = 'a'; // character c
        
          // y implicitly converted to int. ASCII
          // value of 'a' is 97
          x = x + y;
        
          // x is implicitly converted to float
          float z = x + 1.0;
        
          printf("x = %d, z = %f", x, z);
          return 0;
      }
      
      输出:
      x = 107, z = 108.000000
      

      输出:

      x = 107, z = 108.000000
    1. 显式类型转换

      此过程也称为类型转换,它是用户定义的。用户可以在此处键入将结果转换为特定数据类型的结果。

      C语言中的语法:

      (type) expression

      类型表示最终结果将转换为的数据类型。

      // C program to demonstrate explicit type casting
      #include 
        
      int main()
      {
          double x = 1.2;
        
          // Explicit conversion from double to int
          int sum = (int)x + 1;
        
          printf("sum = %d", sum);
        
          return 0;
      }
      
      输出:
      sum = 2
      

      输出:

      sum = 2

      类型转换的优点

      • 这样做是为了利用类型层次结构或类型表示形式的某些功能。
      • 它有助于我们计算包含不同数据类型变量的表达式。

5.尝试以下任何一项:(7 * 1 = 7)

  1. 编写程序以查找1到100之间的阿姆斯壮编号。
    // C program to find Armstrong number
    // from 1 to 100
      
    #include 
      
    /* Function to calculate x raised to the power y */
    int power(int x, unsigned int y)
    {
        if (y == 0)
            return 1;
        if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
        return x * power(x, y / 2) * power(x, y / 2);
    }
      
    /* Function to calculate order of the number */
    int order(int x)
    {
        int n = 0;
        while (x) {
            n++;
            x = x / 10;
        }
        return n;
    }
      
    // Function to check whether the given number is
    // Armstrong number or not
    int isArmstrong(int x)
    {
        // Calling order function
        int n = order(x);
        int temp = x, sum = 0;
        while (temp) {
            int r = temp % 10;
            sum += power(r, n);
            temp = temp / 10;
        }
      
        // If satisfies Armstrong condition
        if (sum == x)
            return 1;
        else
            return 0;
    }
      
    // Driver Program
    int main()
    {
        int i = 1;
      
        for (i = 1; i <= 100; i++)
            if (isArmstrong(i) == 1)
                printf("%d is an ArmStrong Number\n", i);
            else
                printf("%d is not an ArmStrong Number\n", i);
      
        return 0;
    }
    
    输出:
    1 is an ArmStrong Number
    2 is an ArmStrong Number
    3 is an ArmStrong Number
    4 is an ArmStrong Number
    5 is an ArmStrong Number
    6 is an ArmStrong Number
    7 is an ArmStrong Number
    8 is an ArmStrong Number
    9 is an ArmStrong Number
    10 is not an ArmStrong Number
    11 is not an ArmStrong Number
    12 is not an ArmStrong Number
    13 is not an ArmStrong Number
    14 is not an ArmStrong Number
    15 is not an ArmStrong Number
    16 is not an ArmStrong Number
    17 is not an ArmStrong Number
    18 is not an ArmStrong Number
    19 is not an ArmStrong Number
    20 is not an ArmStrong Number
    21 is not an ArmStrong Number
    22 is not an ArmStrong Number
    23 is not an ArmStrong Number
    24 is not an ArmStrong Number
    25 is not an ArmStrong Number
    26 is not an ArmStrong Number
    27 is not an ArmStrong Number
    28 is not an ArmStrong Number
    29 is not an ArmStrong Number
    30 is not an ArmStrong Number
    31 is not an ArmStrong Number
    32 is not an ArmStrong Number
    33 is not an ArmStrong Number
    34 is not an ArmStrong Number
    35 is not an ArmStrong Number
    36 is not an ArmStrong Number
    37 is not an ArmStrong Number
    38 is not an ArmStrong Number
    39 is not an ArmStrong Number
    40 is not an ArmStrong Number
    41 is not an ArmStrong Number
    42 is not an ArmStrong Number
    43 is not an ArmStrong Number
    44 is not an ArmStrong Number
    45 is not an ArmStrong Number
    46 is not an ArmStrong Number
    47 is not an ArmStrong Number
    48 is not an ArmStrong Number
    49 is not an ArmStrong Number
    50 is not an ArmStrong Number
    51 is not an ArmStrong Number
    52 is not an ArmStrong Number
    53 is not an ArmStrong Number
    54 is not an ArmStrong Number
    55 is not an ArmStrong Number
    56 is not an ArmStrong Number
    57 is not an ArmStrong Number
    58 is not an ArmStrong Number
    59 is not an ArmStrong Number
    60 is not an ArmStrong Number
    61 is not an ArmStrong Number
    62 is not an ArmStrong Number
    63 is not an ArmStrong Number
    64 is not an ArmStrong Number
    65 is not an ArmStrong Number
    66 is not an ArmStrong Number
    67 is not an ArmStrong Number
    68 is not an ArmStrong Number
    69 is not an ArmStrong Number
    70 is not an ArmStrong Number
    71 is not an ArmStrong Number
    72 is not an ArmStrong Number
    73 is not an ArmStrong Number
    74 is not an ArmStrong Number
    75 is not an ArmStrong Number
    76 is not an ArmStrong Number
    77 is not an ArmStrong Number
    78 is not an ArmStrong Number
    79 is not an ArmStrong Number
    80 is not an ArmStrong Number
    81 is not an ArmStrong Number
    82 is not an ArmStrong Number
    83 is not an ArmStrong Number
    84 is not an ArmStrong Number
    85 is not an ArmStrong Number
    86 is not an ArmStrong Number
    87 is not an ArmStrong Number
    88 is not an ArmStrong Number
    89 is not an ArmStrong Number
    90 is not an ArmStrong Number
    91 is not an ArmStrong Number
    92 is not an ArmStrong Number
    93 is not an ArmStrong Number
    94 is not an ArmStrong Number
    95 is not an ArmStrong Number
    96 is not an ArmStrong Number
    97 is not an ArmStrong Number
    98 is not an ArmStrong Number
    99 is not an ArmStrong Number
    100 is not an ArmStrong Number
    
  2. 编写程序以生成以下数字结构:
    12345
    1234
    123
    12
    
    #include 
      
    int main()
    {
      
        int i = 0, j = 0;
      
        for (i = 1; i < 5; i++) {
            for (j = 1; j <= 6 - i; j++)
                printf("%d", j);
            printf("\n");
        }
      
        return 0;
    }
    
    输出:
    12345
    1234
    123
    12
    

6.尝试以下任何一项:(7 * 1 = 7)

  1. 编写程序以添加两个尺寸为3 * 3的矩阵,并将结果存储在另一个矩阵中。
    #include 
    #define N 3
      
    // This function adds A[][] and B[][], and stores
    // the result in C[][]
    void add(int A[][N], int B[][N], int C[][N])
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                C[i][j] = A[i][j] + B[i][j];
    }
      
    int main()
    {
        int A[N][N] = { { 1, 1, 1 },
                        { 2, 2, 2 },
                        { 3, 3, 3 } };
      
        int B[N][N] = { { 1, 1, 1 },
                        { 2, 2, 2 },
                        { 3, 3, 3 } };
      
        int C[N][N]; // To store result
        int i, j;
        add(A, B, C);
      
        printf("Result matrix is \n");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                printf("%d ", C[i][j]);
            printf("\n");
        }
      
        return 0;
    }
    
    输出:
    Result matrix is 
    2 2 2 
    4 4 4 
    6 6 6
    
  2. 用C编写一个程序,以创建一个包含五十名学生的数据库,以存储个人详细信息,例如卷号,姓名和分数。打印用户输入姓名的学生的所有详细信息。
    #include 
    #include 
      
    struct Student {
        int roll_no;
        char name[100];
        float marks;
    };
      
    int main()
    {
        int i = 0;
        char n[100];
        struct Student student[50];
      
        for (i = 0; i < 50; i++) {
            printf("\nEnter details for Student %d", i + 1);
      
            printf("\nRoll Number: ");
            scanf("%d", &student[i].roll_no);
      
            printf("\nName: ");
            scanf("%s", student[i].name);
      
            printf("\nMarks: ");
            scanf("%f", &student[i].marks);
        }
      
        printf("\nEnter the name of the student whose details you need: ");
        scanf("%s", n);
      
        for (i = 0; i < 50; i++) {
            if (strcmp(n, student[i].name) == 0) {
      
                printf("\nRoll Number: %d", student[i].roll_no);
      
                printf("\nName: %s", student[i].name);
      
                printf("\nMarks: %f", student[i].marks);
      
                break;
            }
        }
      
        if (i == 50)
            printf("No student found with this name");
    }
    

7.尝试以下任何一项:(7 * 1 = 7)

  1. 用C编写程序以使用指针反转字符串。
    #include 
    #include 
      
    // Function to reverse the string
    // using pointers
    void reverseString(char* str)
    {
        int l, i;
        char *begin_ptr, *end_ptr, ch;
      
        // Get the length of the string
        l = strlen(str);
      
        // Set the begin_ptr and end_ptr
        // initially to start of string
        begin_ptr = str;
        end_ptr = str;
      
        // Move the end_ptr to the last character
        for (i = 0; i < l - 1; i++)
            end_ptr++;
      
        // Swap the char from start and end
        // index using begin_ptr and end_ptr
        for (i = 0; i < l / 2; i++) {
      
            // swap character
            ch = *end_ptr;
            *end_ptr = *begin_ptr;
            *begin_ptr = ch;
      
            // update pointers poisitions
            begin_ptr++;
            end_ptr--;
        }
    }
      
    // Driver code
    int main()
    {
      
        // Get the string
        char str[100] = "GeeksForGeeks";
        printf("Enter a string: %s\n", str);
      
        // Reverse the string
        reverseString(str);
      
        // Print the result
        printf("Reverse of the string: %s\n", str);
      
        return 0;
    }
    
  2. 说明文件操作中的以下功能
    • getw() :getw()函数用于从文件读取整数值。该文件由作为参数传递的指针指向。

      句法:

      int getw(FILE *fp);
    • putw() :putw()函数用于从文件写入整数值。该文件由作为参数传递的指针指向。并且整数值也被指定为参数。

      句法:

      int putw(int number, FILE *fp);
    • fscanf() :fscanf从FILE指针(ptr)指向的文件中读取,而不是从输入流中读取。

      句法:

      int fscanf(FILE *ptr, const char *format, ...) 

      考虑以下文本文件abc.txt

      NAME    AGE   CITY
      abc     12    hyderbad
      bef     25    delhi
      cce     65    bangalore  

      现在,我们只想读取上述文本文件的city字段,而忽略所有其他字段。将fscanf和上面提到的技巧结合起来可以轻松实现此目的。

    • fprintf() :printf用于打印文件中的内容,而不是stdout控制台。
      int fprintf(FILE *fptr, const char *str, ...);