Thread: C Homework

  1. #1
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15

    C Homework

    I have two small programs to write. The only problem, I have no experience with C and very limited Java experience. Sorry if this is long-winded, but I'm at a loss.

    The one I'm working on now is a stats tracker of sorts. I have to take an array of numbers and find the max, min, mean, median, mode, and standard deviation.

    My first problem and the one holding me up currently:

    I'm trying to create the array where the values will be user inputs. First, it should ask the user for the amount of elements in the array. Then it should ask for the elements to fill the array, one at a time. I don't know how any inputs in this language work. I read something about a scanf function but couldn't understand how to use it.

    My second question is in regards to calculating the mode. It's simply as to how would I be able to keep track of the number of times each number occurs in the array, and if several numbers occur the same amount, having them all print. All I can think to do is set up a for loop to go through the array and check to see if the second element is equal to the first. If so, increase the number of a temporary variable for that number. Only the problem doing it that way is counting the same element multiple times. Possibly copying the array into a new one, and if that element is equal to the one it is being compared to, increment the temp variable then removing it from the array? I'm not sure how to go about that, as I'd have to keep track of the number of the elements in the array so that I don't get garbage or anything.

    We were given sample code for qsort, but I can't make heads or tails of that as I still don't understand pointers. So I figured I'd just go the slow route for now and use a for loop to go through the array to sort them. I believe to delete the old array after it's:
    Code:
    delete[] array;
    or is it free?

    There's hardly anything to this code. I just filled in what I knew how to code already.

    Code:
    int main() {
    
        int numElem;
        int array[numElem - 1];
        int i;
        int max;
        int min;
        float mean;
        float median;
        int mode;
        float stdev;
    
    
    
    
    
        //Create the array
        for (i = 0; i < numElem; i++) {
            
            
    
    
    //TODO: Sort the array for easier use
    //TODO: Compute the max
    //TODO: Compute the min
    //TODO: Compute the mean
    //TODO: Compute the median
    //TODO: Compute the mode
    //TODO: Compute the standard deviation
    
    
    
    
    //Prints the computed values next to appropriate lables
    printf("Max: %d\n", max);
    printf("Min: %d\n", min);
    printf("Mean: %d\n", mean);
    printf("Mode: %d\n", mode);
    printf("StDev: %d\n", stdev);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Nice try, but this site still has a homework policy. Read this link.

    You have clearly applied a fair effort into writing the justification for someone else to do your homework. If you apply the same effort to actually do the homework, you will do it well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15
    I'm not asking you to do my homework. I'm just asking if my speculations for the second part is on the right track. As for the first part of my question, I'm just asking how to read user input since we've only worked with random number generation so far and to store it in an array so that I can use it for the rest. If I can learn that part on this program, I'll know how to do my other programs as well. I've already started on the loops of the second program; I just need to see how to read user input.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Perhaps you should have paid a little more attention in class.... Huh?

  5. #5
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15
    If we had covered it in class, then sure. When we're told to learn the language on our own, it can cause a few problems.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    scanf - C++ Reference will probably get you what you want. Pay attention to the return value as well as the format specifier.

    You cannot declare an int for the size of the array, not initialize it, and use it to declare an array. Further, why do you subtract 1?, your for() loop will then go out of bounds of the array.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is possible to sort an array "in place", as long as it is not necessary to subsequently have the array in original order. Yes, that would be done with a loop of some form.

    For reading input, you will need to read up on the scanf() function. Except for char arrays (by convention a char array terminated with a char of value zero is used to represent a string in C), scanf generally reads individual values. But, again, a loop can be used to turn code that reads one value into a code that reads and array.

    In C, the method of allocation is malloc(), calloc(), or realloc() and the method to deallocate is free(). operator delete is not part of C at all, so you should not use it in C code. (In C++, the method of allocation and deallocation must align - if you allocate an array using operator [], you can only release it using operator delete []).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I would presume you have a text to learn from. If not, C, C++ Programming Tutorials - Cprogramming.com , might get you oriented so you're not at a total loss.

    I'm curious how one ends up in a class that doesn't require knowledge of C and expects you to utilize it at an intermediate level?

  9. #9
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15
    Thanks, I'll read up on those.

    As for the -1, I was thinking that since the array starts at 0 and when the user would put in how many elements were to be in the array, they normally wouldn't start counting at 0 as the first space.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    C, C++ Programming Tutorials - Cprogramming.com

    Start reading, I guess. You've got a long way to go syntactically if you don't know how to input values from the keyboard.

    As far as mode goes, it's easy when the data is sorted -- just iterate through the array and check to see if one value is equal to the one before (or after) it, and increment counters accordingly. You don't need a second array. Be careful though, because if you don't do it correctly, you'll end up not counting the first or last values in the array and perhaps end up with the wrong value.

    You do plan on writing individual functions for this, right? That code would be hellish to grade if it was all in main.
    Last edited by Amberlampz; 12-17-2011 at 06:34 PM.

  11. #11
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15
    Our book is on Ubuntu and not C. It's a systems programming class. The school bookstore doesn't carry any programming books, and I don't have a vehicle to go to a bookstore and pick one up until I go home this break. We start off in java, and after 2 java classes, we take data structures which we use c++ and systems programming which uses C, neither include a programming book.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Cesia View Post
    Our book is on Ubuntu and not C. It's a systems programming class. The school bookstore doesn't carry any programming books, and I don't have a vehicle to go to a bookstore and pick one up until I go home this break. We start off in java, and after 2 java classes, we take data structures which we use c++ and systems programming which uses C, neither include a programming book.
    Ouch. 3 languages to learn to deal with data structures?! Mind telling us what insane school this is? Does your school have a library? If so, I'm sure it will have books on these materials.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    If you've taken two courses in Java I don't see how this could be so difficult. You've covered far more extensive and difficult topics than what this asks of you. The thing that might be a little weird is that Java does a lot of stuff "behind the curtains", like default dynamic arrays, and C is far more bare-bones. You have to be very explicit with how you're coding this, especially considering how systems programming is about being efficient.

  14. #14
    Registered User Cesia's Avatar
    Join Date
    Dec 2011
    Posts
    15
    Schools here in Louisiana tend to be a bit odd, after all, our education system has always been on the low side. I was learning VB at the college I went to before I transferred here. Yeah, I'll have to start going to the library more often. I'll be making a trip to BAM over Christmas break and leaving with at least 2 books.

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Odd, one of our more popular tech schools offers introductory to programming with VB.net and has you program a slot machine at the end of the year. Same professor teaches a straight C++ course. And you're in an actual college?

    Sorry to get off topic but I can get back on by saying this. Most of what this task is asking of you can be found on this website without asking anyone, the only catch is that you'll have to read a few tutorials and long threads that may go back 6 months to a year on this board and take pieces of info from all sources to get what you're looking for(each person is most likely struggling with maybe 1 or 2 very specific things in each thread).

    Look at my join date, and a few of the others here. There's a wealth of knowledge here. Just takes a bit of digging to find it. I find it fun however doing it myself and only asking a question once I start going absolutely over-the-edge insane.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help...
    By pSyXaS in forum C Programming
    Replies: 2
    Last Post: 05-08-2011, 02:21 AM
  2. can somebody help me with my homework in c++
    By gosse in forum C++ Programming
    Replies: 19
    Last Post: 04-29-2011, 02:10 PM
  3. Help for Homework!
    By alionas in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 10:36 AM
  4. homework
    By misplaced in forum C++ Programming
    Replies: 18
    Last Post: 10-04-2004, 06:56 AM
  5. I want homework!
    By Tynnhammar in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 02:49 PM