Thread: Storing user input into arrays

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Storing user input into arrays

    Hello. I am stuck on an assignment. Im not looking for the direct answer, more of a guide into the right direction to resolve the issue. I have to take user input of floating point numbers into an array until either 100 numbers have been reached or the user types -999 and then sort the array. i have been able to get to that point but the trouble i am having is not storing the -999 number as that is supposed to just terminate the program. I have tried to put the portion of the code that stores the userInput into the array inside of the If statement but then I get a weird negative number in the 0 index of the array

    Code:
    #include <stdio.h>
    
    
    
    
    float bubble_sort(float list[], int n) {
    	
    	float c, t; 
    	int d;
    	
    	for (c = 0; c < (n - 1); c++)
    	{
    		for (d = 0; d < n - c - 1; d++) 
    		{
    			if (list[d] > list[d + 1])
    			{ 
    				/* Swapping */ 
    				t = list[d];
    				list[d] = list[d + 1]; 
    				list[d + 1] = t;
    			} 
    		} 
    	}
    }
    
    
    int main()
    {
    
    
    	float userInput = 0.0f;
    	float inputArray[100];
    	int i = 0;
    	
    	
    	while (i < 100)
    	{
    		if (userInput != -999)
    		{
    			scanf_s("%f", &userInput);	
    		}
    		inputArray[i] = userInput;
    		i++;
    		
    	}
    	bubble_sort(inputArray, i);
    	
    	
    	printf("%f %f %f", inputArray[0], inputArray[1], inputArray[2]);
    
    
    	
    
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > inputArray[i] = userInput;
    Try putting this inside your if statement.
    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. User Input Arrays Help!
    By Steveqb14 in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2014, 10:03 AM
  2. Storing user input into integer array
    By samii1017 in forum C Programming
    Replies: 2
    Last Post: 10-28-2012, 03:31 PM
  3. Am I reading User input into Arrays correctly?
    By threwup in forum C Programming
    Replies: 9
    Last Post: 02-10-2011, 07:49 PM
  4. Noob question(User input Arrays)
    By wildex999 in forum C++ Programming
    Replies: 12
    Last Post: 02-19-2006, 04:25 PM
  5. initializing arrays with user input
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-17-2001, 12:36 PM

Tags for this Thread