Thread: Class assignment

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Question Class assignment

    Hello, I am new to C and this forum. I am currently attending school for my ASCS. I have an assignment for my programming class that I could use some help with. I figured out the code, although I didn't write it; I was hoping someone could help me make sense of it all. I only have class once a week for a couple hours, so it hasn't quite sunk in yet.

    Code:
    #include <stdio.h>
    
    int largest(int array[10]);
    
    int main()
    {
    	int array[10];
    	int i = 0;
    	int biggest;
    
    	while(i < 10)
    	{
    		array[i] = rand() % 100;
    		printf("%d\n", array[i]);
    		i++;
    	}
    
    	biggest = largest(array);
    
    	printf("the largest number is %d\n", biggest);
    	system("PAUSE");
    	return 0;
    }
    
    int largest(int array[10])
    {
    	int i = 1;
    	int largest;
    
    	largest = array[0];
    	while(i < 10)
    	{
    		if(largest < array[i])
    		{
    			largest = array[i];
    		}
    		i++;
    	}
    	return largest;
    }
    Any help is greatly appreciated. Thanks.

    -Sean

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So you had someone else write your assignment for you, and you want us to explain it to you?

    You better hope the professor/teacher doesn't grade you on your ability to answer questions regarding your assignments. Sometimes test questions are related to the homework assignments. You'll be crippled on the tests if you don't do your own homwork.

    Anyway, I take it that the assignment was to have an array, fill them with random numbers, and then find the largest numbers. That's what it appears the program does.

    BTW, tell your friend that for loops own while loops in these kind of cases.

    And don't be so lazy in the future!

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Well actually I had planned on writing it after I had a better understanding. I wasn't planning on handing this in. This IS the professors code, only I tinkered a little with it, mainly address names. So again, I would be stupid to hand it in, lol. I found the c file in a folder which for some reason wasn't secured with a password.
    Last edited by tmnismo91; 06-16-2007 at 02:30 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Eventually, it's very likely you're going to come up against a brick wall where playing like this isn't going to get you out of trouble, but rather get you into trouble.

    You're going to get burned during the tests, most likely. If that folder is supposed to be passworded, alert the professor, and stay out of it.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Jesus man, I think we got off on the wrong foot. You seem to think I am trying to cheat my out of this. Forget I asked.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Stop for a second and think about this.

    You have an assignment. You also have the answer. From what it sounds like, you just want to slightly alter everything and pass it in. At the very least, you want to use the answer you have as a base for your "real" answer.

    You don't learn how to program this way. I'm trying to explain to you that if you don't work on the assignments from scratch, the work stands a good chance of not making sense to you when you get to the tests. If you think I'm somehow being harsh, you're totally missing my point.

    Conclusion: Throw away the answer. Forget it. Start from scratch. It will help you pass this course and pass it with flying colors.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    This is what I am telling you. I don't know how to write the program based on the instructions. This is the assignment:

    Write a program that puts 10 random numbers into an integer array. The array is then sent to a function called largest; largest will find and return the biggest number in the array. Once returned the biggest number will be printed in main.

    I can create and array of 10 integers, I just don't know how to write the function largest, and so on.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tmnismo91 View Post
    Jesus man, I think we got off on the wrong foot. You seem to think I am trying to cheat my out of this. Forget I asked.
    Howdy Tm,

    I think a few unspoken bits of common sense will sort this out:

    1) C is a lot of work, you get better by working at it. Practice makes well, better if not perfect.
    It's NOT an option, it's a must.

    2) Surely the professor will recognize his own code, even with slight modifications.

    3) If you found this "unsecure" file, probably others did as well. Possibly the professor left it that way for reasons of his own.

    4) Nobody's calling you a cheat, Just a word to the wise to avoid it.

    If you have a question about some part of the code, or your assignment, please post it back up, and let's move on from the accusations.

    Salem has described what the program does. For looping where the number of times the loop is to be executed is known, I also believe a "for" loop, adds to the clarity of the code. Whenever that number is not known in advance, I prefer a "while" loop.

    Edit: OK, the way I like to work with things that I don't know about, is just to work them out by hand with paper and pencil.
    How would you find the largest number in an array, by hand?

    Write down the steps:
    In a loop - because you know you have to look at every number, clearly.

    1) look at the first number. Make it the largest number.
    2) look at the second number, and compare it to the first number
    3) if the second number is bigger than the first number, then the second number becomes the new largest number

    repeat for all numbers. This would be the loop back, part.

    Now take the above, and make it into code: The largest function will have to have access to the array of numbers, so it will need a parameter which includes the array, and it will need to somehow get the answer back up to main OR it could just print out the answer, right inside the largest function. If you choose the latter, then the function could be a void function, which returns no value at all.
    Last edited by Adak; 06-16-2007 at 12:11 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    	while(i < 10)
    	{
    		printf("Is &#37;d smaller than %d\n", largest, array[i] );
    		if(largest < array[i])
    		{
    			largest = array[i];
    			printf("Why yes Dorothy it is, largest is now %d\n", largest );
    		}
    		i++;
    	}
    Just put printf() statements in all the places which confuse you.

    Or learn how to use the debugger, and watch the code execute one line at a time.
    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.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by tmnismo91 View Post
    This is what I am telling you. I don't know how to write the program based on the instructions.
    What you need to do is start from what you do know. Get reference material handy, and keep it with you while you work.

    Quote Originally Posted by tmnismo91 View Post
    I can create and array of 10 integers,
    Great! Start with what you know: Write main() down, and create an array of 10 integers.

    Quote Originally Posted by tmnismo91 View Post
    I just don't know how to write the function largest, and so on.
    Do you know how to write a function? If not, check back to your reference material. If so, write the function prototype out, and write an empty version of the function.

    Then figure out what the heck you're lost at. Make sure you're checking your professor's lecture slides, or the textbook or w/e it is that you have. Work slowly. If you're confused and completely lost, go see the professor or an assistant. Before you go, formulate the question you're going to ask them. Make it clear and concise. If you do that, you stand a better chance of understanding what they tell you, and possibly even answering the question yourself before you ask them.

    I sound pretty harsh at times, but I think you'd rather get it this way than get blindsided on a test. I know what coding is like. You can think you understand a concept just fine, and maybe you do understand it. When it comes time to apply it, if you haven't kept active with it, it's very easy to forget it and be lost. You will be told over and over by programmers that you must actively program in order to learn the skills necessary to get good at it.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Sigh, the professor isn't much help. He has gone over two or three other programs that we were supposed to write ourselves, but he is so vague that the class couldn't without his help. As a result, he wrote it for us on the board. This time around he told us to write it and gave no help. He went and played ping pong checking up on us from time to time. One guy got up and left class he was so ........ed. The other gal managed to get further than me only she works for Intuit and has some programming under her belt. So you see, I know I am fully capable of learning this, but the gap between classes AND the lack of teaching skills isn't helping me much. Its kinda got me wondering if I am throwing my money away taking this course.:gotme:

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    LOL. Whatever. If that's the way it is, then you have to learn from outside material.

    Check the C section of this area from this site:

    http://www.cprogramming.com/tutorial.html

    This article has good diagrams, but a lot of information contained there is incorrect:

    http://www.howstuffworks.com/c.htm

    Combining the two sites could prove useful. If you ask questions on this board, you should be able to move forward nicely.

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Thanks.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    So I created the array of 10 integers and was able to get the integers to print. Now I need to make those integers random, and print the highest integer. Any words of wisdom?

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int array[10];
    	int i = 0;
    
    	while(i < 10)
    	{
    		printf("%d\n", i);
    		i++;
    	}
    	printf("The largest number is %d\n", i);
    
    	system("PAUSE");
    	return 0;
    }

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. read the rand() function description - and try to figure out how to use it
    2. store the values in the array
    3. go through the array and look for the biggest number
    the rest you have
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. i need help with errors in my assignment for class
    By azrael13302473 in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2002, 06:02 PM