Ashwini's Profile

401
Points

Questions
0

Answers
200

  • Asked on January 25, 2019 in c.

    The General Form of a Simple Program
    Let’s take a closer look at below programme and see how we can generalize it a bit. Simple C programs have the form
    directives
    int main(void) {
    statements
    }
    Notice how the braces show where main begins and ends. C uses { and } in much the same way that some other languages use words like begin and end. This illustrates a general point about C: it relies heavily on abbreviations and spe­cial symbols, one reason that C programs are concise .

    Even the simplest C programs rely on three key language features: directives (editing commands that modify the program prior to compilation), functions (named blocks of executable code, of which main is an example), and statements (commands to be performed when the program is run). We’ll take a closer look at these features now.

    #include
    void main()
    {
    int a = 10, b = 20, c;
    printf(“\n original values of a = %d, b = %d”, a, b);
    c = a;
    a = b;
    b = c;
    printf(“\n Swapped Values of a = %d, b = %d”, a, b);

    }
    #include
    It is necessary to “include” information about C’s standard I/O (input/output) library. The program’s executable code goes inside main, which represents the “main” program. The only line inside main is a command to display the desired message, printf is a function from the standard I/O library that can produce nicely for­matted output. The \n code tells printf to advance to the next line after printing the message. The line
    return 0;
    indicates that the program “returns” the value 0 to the operating system when it ter­minates.

    • 347 views
    • 4 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    for while
    Syntax of for loop:

    for(initialization; condition; iteration)
    {
      // body of the loop
    }

    The initialization is an assignment statement that is used to set the loop control variable. The condition is a relational expression that determines when the loop exits. The increment defines how the loop control variable changes each time the loop is repeated. The body of loop can either be empty or a single statement or a block of statements.

    The

    for(initialization; condition; iteration)
    {
      // body of the loop
    }

    is equivalent to

    initialization;
    while(condition)
    {
      // body of the loop
    iteration;
    }

    Syntax of while loop:

    while(condition)
    {
      // body of the loop
    }

    The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop.

    • 295 views
    • 5 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    What is the difference between ‘while’ loop and ‘for’ loop? What is an example of this?
    Originally Answered: What’s the difference between a for loop and a while loop in C?
    Both for loop and while loop are used to excute one or more lines of code certain number of times. The main differences are as follows.
    Syntax:
    While loop:
    while(condtion) {
    //statements to excute.
    }
    For loop:
    for(intialization; condition; Increment or decrement){
    // statements to be excuted.
    }
    There are few variations we can do with for loop.
    Such as all three parts of for loop are optional. That means you can also have a loop like this.
    for(;;) which is nothing but an infinite loop.
    For initialization there can be multiple statements.
    for(i =0, j =0;i

    • 295 views
    • 5 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    sizeof vs strlen()

    Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in C
    Data types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.
    Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable’s type. It doesn’t care about the value of the variable.
    Strlen on the other hand, gives you the length of a C-style NULL-terminated string.
    Summary: The two are almost different concepts and used for different purposes.
    In context of C++: In C++, you do not need any of them as such.
    strlen() in C-style strings can be replaced by C++ std::strings.
    sizeof() in C is as an argument to functions like malloc(), memcpy() or memset() can be replaced by C++ (use new, std::copy(), and std::fill() or constructors).

    • 486 views
    • 5 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    Sizeof operator is a compile time unary operator which can be used to compute the size of its operand.
    The result of sizeof is of unsigned integral type which is usually denoted by size_t.
    sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.
    strlen()

    strlen() is a predefined function in C whose definition is contained in the header file “string.h”.

    strlen() accepts a pointer to an array as argument and walks through memory at run time from the address we give it looking for a NULL character and counts up how many memory locations it passed before it finds one.
    The main task of strlen() is to count the length of an array or string.

    • 486 views
    • 5 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    Let us consider the following XOR list

    A->B->C->D

    suppose you created nodes in this format below

    Key|Link|

    A|0^addr(B)| -> B|addr(A)^addr(C)| -> C|addr(B)^addr(D)| -> D|addr(C)^0|
    CASE #1:[Forward Traversal] Now Suppose you are in B (current_node=>B) want visit C , so you need Address of C . How you will get ?

    Addressof(Next_node) = addressof(Prev_node) ^ Current_node(Link)

    addr(A)^ ( addr(A)^ addr(C) )
    =>(addr(A) ^ addr(A)) ^ addr(C)
    => 0 ^ addr(C)
    =>addr(C)
    CASE #2: [Backward traversal] Now Suppose you are in C (current_node=> C) want visit B , so you need Address of B . How you will get ?

    Addressof(Prev_node) = addressof(Next_node) ^ Current_node(Link)

    addr(D) ^ ((addr(B) ^ addr(D))
    => (addr(D)^ addr(D)) ^ addr(B)
    => 0^addr(B)
    => addr(B)
    Traversing: To traverse whole list ,You will need 3 pointers prevPtr , currPtr , nextPtr to store relative current, previous and next node’s address starting with head. Then in each iteration these pointers need be move to one position ahead.

    struct Node *currPtr = head;
    struct Node *prevPtr = NULL;
    struct Node *nextPtr;

    printf (“Following are the nodes of Linked List: \n”);

    while (currPtr != NULL)
    {
    // print current node
    printf (“%d “, currPtr->key);

    // Save the address of next node
    nextPtr = XOR (prevPtr, currPtr->link);

    //move prevPtr and currPtr one position for next iteration

    prevPtr = currPtr;
    currPtr = nextPtr;

    • 458 views
    • 4 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    XOR has a very special property about it, namely, given a XOR b = c, only two (any two) of the variable are required to compute the the third, with some restrictions. See the XOR swap algorithm for why this works.

    In this case the previous (or next) pointer must still be carried, but only through traversal calculations and not as a seperate member.

    • 458 views
    • 4 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    class ArmstrongExample{
    public static void main(String[] args) {
    int c=0,a,temp;
    int n=153;//It is the number to check armstrong
    temp=n;
    while(n>0)
    {
    a=n%10;
    n=n/10;
    c=c+(a*a*a);
    }
    if(temp==c)
    System.out.println(“armstrong number”);
    else
    System.out.println(“Not armstrong number”);
    }
    }
    Output:
    armstrong number

    • 382 views
    • 4 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Write a program to find all Armstrong number in the range of 0 and 999.

    • 382 views
    • 4 answers
    • 0 votes
  • Asked on January 25, 2019 in c.

    include

    void main()
    {
    char str1[50],str2[50];
    static int i=0;
    int j=0;
    printf(“\nEnter First String\n”);
    gets(str1);
    printf(“\nEnter Second String\n”);
    gets(str2);
    while(str1[i]!=’\0′)
    {
    i++;
    }

    while(str2[j]!=’\0′)
    {
    str1[i]=str2[j];
    j++;
    i++;
    }
    str1[i]=’\0′;
    printf(“\nConcatenated String is %s”,str1);
    }

    • 550 views
    • 6 answers
    • 0 votes