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

📅  最后修改于: 2021-05-20 09:01:36             🧑  作者: Mango

论文下载链接:论文|学期1 | 2015-16

时间: 3小时
总分数:100

注意:-

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

尝试本节中的任何五个问题:(10 * 5 = 50)

2.有哪些不同类型的功能?用C编写一个程序,以升序对学生姓名列表进行排序。

  • 有以下类别:

    1. 不带参数和返回值函数:当函数没有参数,它不从调用函数接收任何数据。类似地,当不返回值时,调用函数不会从被调用函数接收任何数据。
      句法 :
      Function declaration : void function();
      Function call : function();
      Function definition :
                            void function()
                            {
                              statements;
                            }
      
    2. 带有参数的函数,但没有返回值:当一个函数的参数,它接收来自调用函数的任何数据,但它没有返回值。

      句法 :

      Function declaration : void function ( int );
      Function call : function( x );
      Function definition:
                   void function( int x )
                   {
                     statements;
                   }
      
    3. 函数不带任何参数,但返回值:可能有场合,我们可能需要设计可能不带任何参数,但返回一个值给调用函数的功能。一个示例是getchar函数,它没有参数,但它返回一个整数和表示字符的整数类型数据。
      句法 :
      Function declaration : int function();
      Function call : function();
      Function definition :
                       int function()
                       {
                           statements;
                            return x;
                        }
          
    4. 与参数和返回值函数
      句法 :
      Function declaration : int function ( int );
      Function call : function( x );
      Function definition:
                   int function( int x )
                   {
                     statements;
                     return x;
                   }
      

    用C编写的程序以升序对学生姓名列表进行排序

    #include 
    #include 
    #include 
      
    // Defining comparator function as per the requirement
    static int myCompare(const void* a, const void* b)
    {
      
        // setting up rules for comparison
        return strcmp(*(const char**)a, *(const char**)b);
    }
      
    // Function to sort the array
    void sort(const char* arr[], int n)
    {
        // calling qsort function to sort the array
        // with the help of Comparator
        qsort(arr, n, sizeof(const char*), myCompare);
    }
      
    int main()
    {
      
        // Get the array of names to be sorted
        const char* arr[]
            = { "geeksforgeeks", "geeksquiz", "clanguage" };
      
        int n = sizeof(arr) / sizeof(arr[0]);
        int i;
      
        // Print the given names
        printf("Given array is\n");
        for (i = 0; i < n; i++)
            printf("%d: %s \n", i, arr[i]);
      
        // Sort the given names
        sort(arr, n);
      
        // Print the sorted names
        printf("\nSorted array is\n");
        for (i = 0; i < n; i++)
            printf("%d: %s \n", i, arr[i]);
      
        return 0;
    }
    

    3.写一篇关于自上而下的程序开发方法的简短说明。

  • 这种方法指的是程序的开发,其中根据任务将程序分为子程序。这些任务称为模块。 C语言支持这种方法,程序员也认为这种方法是一种以分层方式创建项目的好方法。在此首先开发主要模块,然后再开发其他模块,依此类推。

    4.用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 positions
            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;
    }
    

    5.使用适当的示例写出按值调用和按引用调用之间的差异

  • 参数数据可以通过不同的方法传入和传出方法和函数。让我们假设从另一个函数A()调用了函数B () 。在这种情况下, A被称为“呼叫者函数”,B被称为“被呼叫函数或被调用者函数” 。同样, A发送给B的参数称为实际参数,而B的参数称为形式参数

    术语

    • 形式参数:在函数或方法的原型中出现的变量及其类型。
    • 实际参数:与在调用环境中的函数或方法调用中出现的形式参数相对应的变量或表达式。
    • 模式:
      • IN:将信息从呼叫者传递到被叫者。
      • OUT:被叫方将值写入调用方。
      • 输入/输出:呼叫者告诉被呼叫者变量的值,该值可以由被呼叫者更新。

    参数传递的重要方法

    1. 按值传递:此方法使用模式内语义。对形式参数所做的更改不会传输回调用方。对被调用函数或方法内部的形式参数变量的任何修改仅影响单独的存储位置,并且不会反映在调用环境中的实际参数中。此方法也称为按值调用

      // C program to illustrate
      // call by value
      #include 
        
      void func(int a, int b)
      {
          a += b;
          printf("In func, a = %d b = %d\n", a, b);
      }
      int main(void)
      {
          int x = 5, y = 7;
        
          // Passing parameters
          func(x, y);
          printf("In main, x = %d y = %d\n", x, y);
          return 0;
      }
      

      输出:

      In func, a = 12 b = 7
      In main, x = 5 y = 7
      

      诸如C,C++, Java之类的语言都支持这种类型的参数传递。实际上, Java严格按值调用。
      缺点:

      • 存储分配效率低下
      • 对于对象和数组,复制语义是昂贵的
    2. 通过引用传递(别名):此技术使用输入/输出模式语义。对形式参数所做的更改确实会通过参数传递回传给调用者。形式参数的任何更改都会反映在调用环境中的实际参数中,因为形式参数会收到对实际数据的引用(或指针)。此方法也称为通过引用调用< em> 。该方法在时间和空间上都是有效的。

      // C program to illustrate
      // call by reference
      #include 
        
      void swapnum(int* i, int* j)
      {
          int temp = *i;
          *i = *j;
          *j = temp;
      }
        
      int main(void)
      {
          int a = 10, b = 20;
        
          // passing parameters
          swapnum(&a, &b);
        
          printf("a is %d and b is %d\n", a, b);
          return 0;
      }
      

      输出:

      a is 20 and b is 10
      

      C和C++都支持按值调用以及按引用调用,而Java不支持按引用调用。
      缺点:

      • 可能发生许多潜在的情况
      • 程序有时很难理解

    6.算法的特点是什么?编写算法以查找11到50之间被3整除的所有数字的总和。

  • 算法的特征
    • 清晰明确:算法应清晰明确。它的每个步骤在所有方面都应该明确,并且只能导致一种含义。
    • 定义明确的输入:如果算法说要接受输入,则它应该是定义明确的输入。
    • 定义明确的输出:该算法必须明确定义将要产生的输出,并且也应定义明确。
    • 有限度:算法必须是有限的,即它不应以无限循环或类似形式结束。
    • 可行:该算法必须简单,通用且实用,以便可以根据可用资源执行该算法。它不得包含任何未来的技术或任何东西。
    • 与语言无关设计的算法必须与语言无关,即它必须只是可以用任何语言实现的简单指令,但输出将与预期的一样。

    查找所有11到50之间可被3整除的数字之和的算法

    1. Traverse the numbers between 11 and 50
    2. For each number check if it is divisible by 3
    3. If divisible by 3, add them under variable sum
    4. Print the sum at the end
    

    7.用C中的相关示例说明并比较静态和外部存储类。

    1. extern :extern存储类仅告诉我们该变量是在其他位置定义的,而不是在使用该变量的同一块中定义的。基本上,该值是在另一个块中分配给它的,也可以在另一个块中对其进行覆盖/更改。因此,extern变量不过是一个全局变量,该变量在声明有合法值的情况下进行了初始化,以便在其他地方使用。可以在任何函数/块中访问它。同样,也可以通过在任何函数/块的声明/定义之前将“ extern”关键字放置在普通全局变量中,将其设置为外部变量。这基本上表示我们不是在初始化新变量,而是仅在使用/访问全局变量。使用外部变量的主要目的是可以在大型程序的两个不同文件之间访问它们。有关外部变量如何工作的更多信息,请查看此链接。
    2. static :此存储类用于声明静态变量,这些静态变量在用C语言编写程序时经常使用。静态变量具有保留其值的属性,即使它们超出其范围也是如此!因此,静态变量在其范围内保留了其最后一次使用的值。因此,我们可以说它们仅被初始化一次,并且一直存在到程序终止为止。因此,没有分配新的内存,因为它们没有被重新声明。它们的范围对于定义它们的函数是局部的。全局静态变量可以在程序中的任何位置访问。默认情况下,编译器将它们分配为值0。
    // A C program to demonstrate different storage
    // classes
    #include 
      
    // declaring the variable which is to be made extern
    // an intial value can also be initialized to x
    int x;
      
    void autoStorageClass()
    {
      
        printf("\nDemonstrating auto class\n\n");
      
        // declaring an auto variable (simply
        // writing "int a=32;" works as well)
        auto int a = 32;
      
        // printing the auto variable 'a'
        printf("Value of the variable 'a'"
               " declared as auto: %d\n",
               a);
      
        printf("--------------------------------");
    }
      
    void registerStorageClass()
    {
      
        printf("\nDemonstrating register class\n\n");
      
        // declaring a register variable
        register char b = 'G';
      
        // printing the register variable 'b'
        printf("Value of the variable 'b'"
               " declared as register: %d\n",
               b);
      
        printf("--------------------------------");
    }
      
    void externStorageClass()
    {
      
        printf("\nDemonstrating extern class\n\n");
      
        // telling the compiler that the variable
        // z is an extern variable and has been
        // defined elsewhere (above the main
        // function)
        extern int x;
      
        // printing the extern variables 'x'
        printf("Value of the variable 'x'"
               " declared as extern: %d\n",
               x);
      
        // value of extern variable x modified
        x = 2;
      
        // printing the modified values of
        // extern variables 'x'
        printf("Modified value of the variable 'x'"
               " declared as extern: %d\n",
               x);
      
        printf("--------------------------------");
    }
      
    void staticStorageClass()
    {
        int i = 0;
      
        printf("\nDemonstrating static class\n\n");
      
        // using a static variable 'y'
        printf("Declaring 'y' as static inside the loop.\n"
               "But this declaration will occur only"
               " once as 'y' is static.\n"
               "If not, then everytime the value of 'y' "
               "will be the declared value 5"
               " as in the case of variable 'p'\n");
      
        printf("\nLoop started:\n");
      
        for (i = 1; i < 5; i++) {
      
            // Declaring the static variable 'y'
            static int y = 5;
      
            // Declare a non-static variable 'p'
            int p = 10;
      
            // Incrementing the value of y and p by 1
            y++;
            p++;
      
            // printing value of y at each iteration
            printf("\nThe value of 'y', "
                   "declared as static, in %d "
                   "iteration is %d\n",
                   i, y);
      
            // printing value of p at each iteration
            printf("The value of non-static variable 'p', "
                   "in %d iteration is %d\n",
                   i, p);
        }
      
        printf("\nLoop ended:\n");
      
        printf("--------------------------------");
    }
      
    int main()
    {
      
        printf("A program to demonstrate"
               " Storage Classess in C\n\n");
      
        // To demonstrate auto Storage Class
        autoStorageClass();
      
        // To demonstrate register Storage Class
        registerStorageClass();
      
        // To demonstrate extern Storage Class
        externStorageClass();
      
        // To demonstrate static Storage Class
        staticStorageClass();
      
        // exiting
        printf("\n\nStorage Classess demonstrated");
      
        return 0;
    }
      
    // This code is improved by RishabhPrabhu
    

    8.说明指针的功能。编写一个C程序以使用指针对给定的数字进行排序。

  • 指针功能:
    1. 指针可节省内存空间。
    2. 指针的执行时间更快,因为数据是通过地址操纵的,即直接访问
      内存位置。
    3. 使用指针可以有效地访问内存。指针分配并释放内存空间。内存是动态分配的。
    4. 指针与数据结构一起使用。它们对于表示二维和多维很有用
      数组。
    5. 我们可以访问任何类型的数组的元素,无论其下标范围如何。
    6. 指针用于文件处理。
    7. 指针用于动态分配内存。
    8. 在C++中,声明为基类的指针可以访问派生类的对象。但是,指向派生类的指针不能访问基类的对象。编译器将生成一条错误消息“无法将’A *转换为B *,’”,其中A是基类,而B是派生类。

    9.区分以下内容:

    • 高级语言和低级语言
      • 高级语言几乎是人类语言。换句话说,这些是用比人类更容易理解的语言编写代码的语言。
      • 低级语言几乎是机器语言。换句话说,这些语言是用机器比人类更能理解的语言编写代码的。
    • 链接器和加载器

      链接器和加载器之间的区别

      Linker Loader
      Linker generates the executable module of a source program. Loader loads the executable module to the main memory for execution.
      Linker takes the object code generated by an assembler, as input. Loader takes executable module generated by a linker as input.
      Linker combines all the object modules of a source code to generate an executable module. Loader allocates the addresses to an executable module in main memory for execution.
      The types of Linker are Linkage Editor, Dynamic linker. The types of Loader are Absolute loading, Relocatable loading and Dynamic Run-time loading.
    • 逻辑错误和运行时错误
      1. 运行时错误:成功编译后在程序执行(运行时)期间发生的错误称为运行时错误。最常见的运行时错误之一是被零除,也称为除法错误。这些类型的错误很难找到,因为编译器没有指向发生错误的行。
        为了获得更多的理解,请运行下面给出的示例。
        // C program to illustrate
        // run-time error
        #include 
        void main()
        {
            int n = 9, div = 0;
          
            // wrong logic
            // number is divided by 0,
            // so this program abnormally terminates
            div = n / 0;
          
            printf("resut = %d", div);
        }
        

        错误:

        warning: division by zero [-Wdiv-by-zero]
             div = n/0;

        在给定的示例中,存在除以零的误差。这是运行时错误的示例,即运行程序时发生的错误。

      2. 逻辑错误:在编译和执行程序时,如果给出某些输入值,则无法获得所需的输出。这些类型的错误提供了不正确的输出,但看起来没有错误,被称为逻辑错误。这些是编程初学者最常见的错误之一。
        这些错误仅取决于程序员的逻辑思维,很容易检测我们是否遵循执行路线并确定程序为何采用该执行路径。
        // C program to illustrate
        // logical error
        int main()
        {
            int i = 0;
          
            // logical error : a semicolon after loop
            for (i = 0; i < 3; i++)
                ;
            {
                printf("loop ");
                continue;
            }
            getchar();
            return 0;
        }
        

        无输出