Thread: Project-Sorting and Change by Reference

  1. #1
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16

    Project-Sorting and Change by Reference

    My code so far.
    prompt: The program then takes the following approach to sort the numbers in the array:

    1. it stores the value of n in a temporary variable temp,
    2. it searches for the largest number in the array, starting with position 0 and ending with position temp-1 (use the previous algorithm for finding the largest element of an array),
    3. it then switches the field that stores the largest value with the field indexed by temp-1,
    4. the program decrements temp by 1 (temp--),
    5. the program repeats steps b-d until temp is decremented to 1 in step d.

    The program then prints the numbers of the array to the screen on a single line. Please note that in step c., the program does not need to switch values if the largest value is already located in temp-1. Also, for this program, use the previously implemented algorithm for finding the largest value in a set of numbers but apply it to an array and implement it as a function with the following signature:
    Had to email him already asking clarification. My code so far. I believe I got it to sort correctly and now I need to have it change (step C). Professor gave us hint of change by reference not value. Didn't think we could do that in C
    Code:
    #include <stdio.h>
    int main(){
        int i,n,j;
        float arr[100];
        printf("Enter total number of elements(1 to 100): ");
        scanf("%d",&n);
        printf("\n");
        for(i=0;i<n;++i)  /* Stores number entered by user. */
        {
           printf("Please enter Number %d: ",i+1);
           scanf("%f",&arr[i]);
        }
        for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
        {
           if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
               arr[0]=arr[i];         
    
        }
        for(i=1;i<n;++i)
        if(arr[0] >= n)
             {arr[i]==n;
             n - 1;
             }
        printf("Largest element = %.2f",arr[0]);
        printf("\n"); //Drop a line
        printf("%i",arr[i]);
        
            return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The algorithm that you have in mind is essentially selection sort, except that you select the current largest rather than the current smallest.

    The "Loop to store largest number to arr[0]" looks wrong: according to the algorithm outline, you want to store the largest number to arr[temp - 1] instead. I do not understand what you are trying to accomplish with the second loop.

    Quote Originally Posted by marchmadness
    Professor gave us hint of change by reference not value. Didn't think we could do that in C
    Have you been taught functions and pointers yet?
    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
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Laserlight,
    Thank you for the loop to store arr temp -1 instead.
    Second loop, my goal was to use IF statements to do a pass by reference to change the value in array that is largest to temp -1 when found before minus by 1.

    Another question to throw in here, how exactly do you print an array in one line?
    I know if you use brackets for when you know the iput and it isn't user you can do that. BUt not sure how to with user input.
    Professor taught us functions and pointers very vaguely. THat will be the next project.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marchmadness
    Thank you for the loop to store arr temp -1 instead.
    Second loop, my goal was to use IF statements to do a pass by reference to change the value in array that is largest to temp -1 when found before minus by 1.
    Your loops should be nested instead. Here is pseudocode based on the algorithm outline:
    Code:
    temp = n
    while temp > 1
        largest = 0
        for i = 1 to i = temp - 1
            if numbers[i] > numbers[largest]
                largest = i
        if largest != temp - 1
            swap numbers[largest] and numbers[temp - 1]
        decrement temp
    print numbers
    It is not specified how exactly you should go about recording the current largest, so I chose to save the index of the current largest as this makes it convenient to satisfy "the program does not need to switch values if the largest value is already located in temp-1". This could also be what your professor was hinting about "references", since an index does refer to an element, in a sense.

    Note that in C, the "to i = temp - 1" part of the pseudocode for loop is normally accomplished by a "i < temp" condition rather than "i <= temp - 1".

    Quote Originally Posted by marchmadness
    Another question to throw in here, how exactly do you print an array in one line?
    I know if you use brackets for when you know the iput and it isn't user you can do that. BUt not sure how to with user input.
    Here is pseudocode for printing an array on a single line:
    Code:
    for i = 0 to i = n - 1
        print numbers[i] followed by a space (or some other separator other than a new line character)
    print a new line character
    EDIT:
    Oh wait, it looks like you posted this, but ignored it in your code:
    Also, for this program, use the previously implemented algorithm for finding the largest value in a set of numbers but apply it to an array and implement it as a function with the following signature:
    You would need to tell us what is this function's declaration and what is this mysterious "previously implemented algorithm", though I doubt it will differ much from my pseudocode, unless the algorithm saves the value rather than the index. I expect that this is actually what the "change by reference not value" refers to. Basically, there would be no change to my pseudocode hint, except that a chunk of the code within the while loop would be moved to its own function when you implement it.
    Last edited by laserlight; 10-29-2015 at 01:58 PM.
    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

  5. #5
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Response to the edit: The "previously implemented alogrithm" does not exist haha... My professor told us he copied and pasted problems from different professors... Didn't realize he left that line still in there.
    Thank you so far.
    Went to a tutor a bit later that night.
    My code so far. Debating about nested loop or this approach.
    Code:
    #include <stdio.h>
    int main(){
        int i,n,j, largest, temp, List[100], SlotofLarge, slot, swap, list;
        int getMax (int *list, int n); 
        float arr[100];
        
        printf("Enter total number of elements(1 to 100): ");
        scanf("%d",&n);
        printf("\n");
    for(i=1; i = temp; ++i)  
        {
           printf("Please enter Number %d: ",i+1);
           scanf("%f",&arr[i]);
        } 
    for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
        {
           if(largest<arr[i]) 
               largest=arr[i];
        }
                
       temp = n;
    for(i=0; i<n;){
    
    SlotofLarge = getMax(int list, temp);
    swap = List[temp-1];
    List[temp-1] = List[slot];
    List[slot] = swap;
    temp--;
    }
    return 0;
    }

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Seriously: a) find a new tutor; and b) find a new professor (this may be harder to do)

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Actually, give me the email address of your professor. I'll email them (without mentioning your name).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marchmadness
    Debating about nested loop or this approach.
    What is this approach that you have in mind?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Master Project for reference?
    By cj31387 in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2011, 06:32 AM
  2. After sorting can`t change bool value
    By fukki in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2010, 07:37 AM
  3. change the target of a reference
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2008, 06:28 AM
  4. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 03:19 PM
  5. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 01:21 PM

Tags for this Thread