Like Tree1Likes
  • 1 Post By omGeeK

completely lost

This is a discussion on completely lost within the C++ Programming forums, part of the General Programming Boards category; Here is my problem: Write a program that reads in a set of positive integers, representing test scores for a ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    10

    completely lost

    Here is my problem:
    Write a program that reads in a set of positive integers, representing test scores for a class, and outputs how many times a particular number appears in the list. You may assume that the data has a set at most 100 numbers and that -999 marks the end of the input data. The numbers must be output in increasing order: Example data:
    55 80 78 92 95 55 78 53 92 65 78 95 85 92 85 95 95
    output should look like:

    Test Score Count
    53 1
    55 2
    65 1
    78 3
    80 1
    85 2
    92 3
    95 4

    I started trying and am completely lost first I am trying just to get the data stored and to print out in order, not trying to do any kinda count yet until I can store and sort the data. This is what I have done but I know it isn't even close: I am not completely sure how arrays work just yet:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void testSort(int list[], int length);
    void fillArray(int list[], int length);
    void initialize(int list[], int length);
    
    
    int main ()
    {
    	int test[100];
    	int i;
    	int lengthx;
    	int score = 0;
    	int length = 100;
    
    
    	cout << "Enter how many test scores will be entered:";
    	cin >> lengthx;
    	cout << endl;
    
    
    	initialize(test, length);
    
    
    	cout << "Enter " << lengthx << " test scores: ";
    	
        fillArray(test, lengthx);
    
    
    	testSort(test, lengthx);
    
    
    	for ( i = 0; i < length; i++)
    		cout << test[i] << " ";
    		cout << endl;
    
    
    		return 0;
    }
    
    
    void testSort(int list[], int length)
    {
    	int temp;
    	int iteration;
    	int index;
    
    
    	for (iteration = 0; iteration < length; iteration++)
    	{
    		for (index = 0; index < length - iteration; index++)
    			if (list[index] > list[index + 1])
    			{
    				temp = list[index];
    				list[index] = list[index+1];
    				list[index + 1] = temp;
    			}
    	}
    }
    
    
    void fillArray(int list[], int length)
    {
    	int index;
    
    
    	for (index = 0; index < length; index++)
    	cin >> list[index];
    }
    
    
    void initialize(int list[], int length)
    {
    	int index;
    
    
    	for (index = 0; index < length; index++)
    		list[index] = 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    seems you have trouble in sorting
    a bubble sort should be like this:
    Code:
        for (iteration = 0; iteration < length; iteration++)
        {
            for (index = 0; index < length - iteration-1; index++) // i put -1 on it because if you're not doing it, in the last loop, it'll access the 'length' index(which is nothing)
                if (list[index] > list[index + 1])
                {
                    temp = list[index];
                    list[index] = list[index+1];
                    list[index + 1] = temp;
                }
        }
    Last edited by aquatorrent; 04-05-2012 at 09:28 AM. Reason: testing the code :P

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    It seems the real problem is that you're in over you head here... Is it possible to alter the solution to something bruter jet functional. Well you know, so that you can get it running, but without and fancy pancy stuff?

    quasicode
    Code:
    results[maxScore] = 0, declare an array with length maxScore and set all instances to zero.
    loop until score is negative
       result[score]++;
    Now you have an array looking like this.
    Code:
    results = {3, 0, 2, 1, etc...}
    3 students scored zero points and so on.
    Last edited by überfuzz; 04-05-2012 at 09:36 AM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
    int test[100] = {0};
    Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.
    rags_to_riches likes this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    Quote Originally Posted by omGeeK View Post
    Code:
    int test[100] = {0};
    Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.
    Sure, but I've seen people who believe that "int test[100] = {4};" initialises all elements of test to the value 4, and get into a arguments that the compiler is at fault when it doesn't.

    Much easier to avoid such silly debates entirely by either initialising the whole array explicitly (i.e. 100 values to initialise a 100 element array) or write code to overtly initialise or reinitialise the array.
    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, Sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    True, but if you know you want to initialize to zero why not lol

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    I don't know whether your debate is over my quasi code or not. I was merely trying to point out a possible solution. Cheers!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by omGeeK View Post
    Code:
    int test[100] = {0};
    Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.
    I've seen a compiler (VS2005 I believe it was) add 4MB to the size of an executable, for a 4MB array I had initialised that way.
    Using ZeroMemory instead dropped it down to just a few hundred KB.

    I thoroughly recommend it for small arrays such as this, but be careful doing it for something quite large.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by iMalc View Post
    I've seen a compiler (VS2005 I believe it was) add 4MB to the size of an executable, for a 4MB array I had initialised that way.
    is it possible that it was declared in the global scope or in a namespace, not in a function or class?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Elkvis View Post
    is it possible that it was declared in the global scope or in a namespace, not in a function or class?
    I think it was a static class member, but I could be wrong.
    Last edited by iMalc; 04-06-2012 at 04:04 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Completely lost in C#
    By rvbalplaya in forum C# Programming
    Replies: 5
    Last Post: 11-08-2005, 11:49 AM
  2. I'm completely stuck!
    By tek in forum C++ Programming
    Replies: 0
    Last Post: 08-23-2003, 07:43 PM
  3. I think i jacked this up completely. Help?
    By correlcj in forum C Programming
    Replies: 5
    Last Post: 07-24-2002, 04:17 AM
  4. Completely OOP
    By golfinguy4 in forum C# Programming
    Replies: 4
    Last Post: 04-14-2002, 12:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21