Thread: progress since Feb 2018

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    progress since Feb 2018

    Well, i've finished my 3rd book, not quite ready for K&R plus I don't have the $40 right now to purchase it. I am using Google Books app and using clang on FreeBSD 11, haven't dipped into Windows Dev yet but I have tried Code::Blocks at the library on a USB. I am ssh'ing into my FreeBSD vps and doing all the compiling in terminal.

    I've started answering a chapter of questions after not writing much code over the summer, just re-reading a bunch of material. The code below are two quiz questions (Lesson 8 out of "SAMS Teach Yourself C Programming in 24 hours a Day by Bradley L. Jones., blue cover text) I've also read & completed The C Book - Table of Contents

    I was able to successfully answer all the questions in this chapter of the text, something that would have been daunting a few months back.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int random_array[1000];
    int a, b;
    
    long total;
    
    int main(void)
    {
        srand(time(NULL));
    
        for(a=0;a<1000;a++)
        {
            random_array[a] = rand();
            total += random_array[a];
        }
    
        printf("Average of 1000 arrays: %ld", total/1000);
    
        for(a=0;a<1000;a++)
        {
            if(a%10 == 0)
            {
                printf("\nPress ENTER to continue or Ctrl+C to quit...\n");
                getchar();
                continue;
            }
            printf("\nrandom_array[%d] = ", a);
            printf("%d", random_array[a]);
        }
    
        return 0;
    }
    And the second quiz I completed successfully

    Code:
    #include <stdio.h>
    
    int a;
    int my_array[10];
    int my_new_array[10];
    int complete;
    
    int main()
    {
        for(a=0;a<10;a++)
        {
            my_array[a] = a;
        }
    
        for(a=0;a<10;a++)
        {
            printf("my_array[%d] = %d\n", a, my_array[a]);
        }
    
        printf("\nNow....copy values to new array and add 10 to each value...");
        getchar();
    
        for(a=0;a<10;a++)
        {
            my_array[a] = my_new_array[a+10];
            printf("\nmy_new_array[%d]", a+10);
        }
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One major improvement that you can make: turn all your global variables into local variables.

    Also, are you sure that you need the continue in your first program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Two general comments.
    1. Don't use global variables, move them into main (and initialise them).

    2. You normally put \n at the end of each printf (not at the beginning). The stdout stream is line buffered, which means it is seen soonest after every \n printed.

    Both programs have bugs.

    > printf("\nPress ENTER to continue or Ctrl+C to quit...\n");
    See the continue two lines later?
    Check your output, you seem to be missing every 10th entry in your array.

    > my_array[a] = my_new_array[a+10];
    Yes you print the right answer, but your arrays are all out of bound accesses with that +10 inside the [ ]
    Not to mention that you probably wanted my_new_array on the left hand side.
    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

Similar Threads

  1. So long 2016, it was nice knowing you
    By Salem in forum Party Board
    Replies: 1
    Last Post: 01-01-2017, 12:15 PM
  2. need help in progress bar
    By cth_56400 in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2012, 10:45 PM
  3. Progress bar
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 02-26-2008, 03:49 PM
  4. Progress Bar
    By mikeman118 in forum Windows Programming
    Replies: 3
    Last Post: 11-06-2007, 10:16 PM

Tags for this Thread