Thread: Need help in algorithm/coding

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Need help in algorithm/coding

    Hi. I am new in C programming. I am tasked to complete a program. The hardest part in programming is to understand the logic of a program. Please explain to how and why the algorithm works. I have commented the parts that i need know of/needs help in green. Your help is so much appreciated. Thanks.

    These are the requirements:
    Complete the following C program so that it displays the addresses of

    1. functions addr, f1, f2, and main;
    2. all string literals such as "Hello, world!";
    3. all initialized global variables;
    4. all un-initialized global variables;
    5. all dynamically allocated memories;
    6. all formal parameters in functions;
    7. all local variables;
    8. start and end of its command line arguments;
    9. start and end of its environment (variables and values).

    The sample program is shown below.

    Code:
    // To declare package/lib
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    extern char **environ;
    
    int global_x = 10; // initialised global variable
    int global_y; // un-initialised global variable
    char global_array1[] = "Hello, world!"; // initialised global array
    char global_array2[10]; // un-initialised global array
    char *global_pointer1 = "bye!"; // global pointer to a string literal
    char *global_pointer2; // un-initialised global pointer
    float global_float = 100.1; // initialised global variable
    double global_double; // un-initialised global variable
    
    // To define global variable
    #define ONEGB 1073741824
    #define ONEMB 1048576
    #define ONEKB 1024
    
    char *addr(unsigned long a)
    {
    
    unsigned long r; // remainder /* ---> Why does division have to do with finding a address?? r = (unsigned long) a; int gb = (int) ( r / ONEGB ); r -= gb * ONEGB; int mb = (int) ( r / ONEMB ); r -= mb * ONEMB; int kb = (int) ( r / ONEKB ); r -= kb * ONEKB; int b = (int) ( r ); */ // To dynamically allocated memory char *p = malloc(64); // To store value in p. --> Why does"%4dGB" means and how it works. Why can't to be just %d?? sprintf(p, "%4dGB, %4dMB, %4dKB, %4d", gb, mb, kb, b); return p;
    } int f2(int x) {
    char * f2_p; int f2_x = 21; f2_p = malloc(1000); // A BLANK part. What do i need to do here???? ..... // What is L:?? Is it a variable to be declared in the BLANK part? L: f2_x = 10; return f2_x;
    } // To declare a function void f1(int x1, int x2, float x3, char x4, double x5, int x6) {
    int f1_x = 10; int f1_y; char *f1_p1 = "This is inside f1"; // Create pointer to another string char *f1_p2; f1_p2 = malloc(100); ..... // What do i need to do here?? f1_y = f2(10); return;
    } main(int argc, char *argv[]) {
    /* What do i need to do here?? ..... f1( .... ); */
    exit(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Why does division have to do with finding a address??
    It doesn't.
    All it is doing is reducing a value into components. Like 1234 / 1000 gives you 1, and then remainder becomes 234.

    > Why does"%4dGB" means and how it works.
    > Why can't to be just %d??
    You can change it to %d if you want, but the output won't be arranged in neat columns.

    > 1. functions addr, f1, f2, and main;
    Well the very first call could be something like
    Code:
    printf("Addr is at %s\n", addr( (unsigned long )&addr  );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed