GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2008, 17:13
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

input sorting program


Hello I have a code that works very well when I asign values to my array, but when I set it for the user to input the values the program crashes.

Here is my code:
CPP / C++ / C Code:
// Header files
#include "stdlib.h"
#include "stdio.h"
#include "iostream.h"

int compare_int(int *a, int *b){

  if (*a < *b)
	return(-1) ;
  else if (*a == *b)
	return(0) ;
  else
	return(1) ;
}

int main(){

  int int_values[] = {0} ;  
   
  int elements = 10, i ;

  cout << "Please enter 10 numbers " ;
  cout << " any order\n" ;
  cin >> int_values ;
   
// This is where I assume the problem is:
  qsort(int_values, elements, 
	sizeof(int), (int (*) 
	(const void *, const void *))
	compare_int) ;

  for (i = 0; i < elements; i++){
    cout << int_values[i] << " " ;
  }

  cout << "\n" ;
  return 0 ;
}

As my note states I have found that the problem must be with the algorithm. I found this by taking it out to see if the input was being stored and it is.

Thank you for any help
Gene Saika
  #2  
Old 21-Nov-2008, 17:23
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: input sorting program


Quote:
Originally Posted by genesaika
CPP / C++ / C Code:
...
  int int_values[] = {0} ;  
...
  cout << "Please enter 10 numbers " ;
  cout << " any order\n" ;
  cin >> int_values ;
This should tell you something. If you need explicit hints:
  • What is the size of the array?
  • How are you writing values into each of the array's elements?
  #3  
Old 21-Nov-2008, 17:29
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: input sorting program


The array's size is 11 it has 10 elements and I was confused about how each element was being asigned. My first thought was " shouldn't there be something telling this program that each number is a new element?," but I couldn't find the way to do it and after having many people look over it I feel comfortable saying that this is not likely the issue, these people would include many classmates and my teacher.
  #4  
Old 21-Nov-2008, 19:26
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: input sorting program


Quote:
Originally Posted by genesaika
The array's size is 11...
No, you are specifying that the array has only one element:
Quote:
CPP / C++ / C Code:
int int_values[] = { 0 } ;
By the fact that the array is unbounded (int_values has no value within the square brackets...), the compiler defers to the number of elements specified in the initialization block to determine how many elements are within the array. In this case, only one value is specified, so the array will only have one element.

If you want the array to have 11 elements where they are all initialized to zero, use the following:
CPP / C++ / C Code:
int int_values[11] = { 0 };
Quote:
...when I set it for the user to input the values the program crashes.
...which is most likely due to the fact that you may have been writing values past the end of the array which trashed other values maintained on the stack.
Quote:
My first thought was " shouldn't there be something telling this program that each number is a new element?," but I couldn't find the way to do it...
The following line should have been a clue that life had gone south:
Quote:
CPP / C++ / C Code:
cin >> int_values;
int_values is an address to an integer. cin is not overloaded to write to type int*.

So to answer your question about how to write to each element within an array, treat each element separately:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main() {
    const int size = 8;
    int a[size] = { 0 };

    for (int i = 0; i < size; ++i)
        cin >> a[i];

    return 0;
}
Last edited by ocicat : 21-Nov-2008 at 20:13.
  #5  
Old 22-Nov-2008, 00:17
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: input sorting program


After testing your suggestions in my code I am faced with yet another issue. The input is set to zero for all of my array's elements

Here is the code that was modified:
CPP / C++ / C Code:
int int_values[11] = {0} ;  
   
  int elements = 10, i ;
  const int size = 10;
  int a[size] = { 0 };

  
  cout << "Please enter 10 numbers " ;
  cout << " any order\n" ;
  for (int i = 0; i < size; ++i){
     cin >> a[i] ;
  }
  #6  
Old 22-Nov-2008, 00:32
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: input sorting program


Quote:
Originally Posted by genesaika
Here is the code that was modified...
Since it is unclear as to whether you have changed your code or mine, there isn't much anyone can say without seeing complete code which will compile.

For example:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    const int size = 4;
    int a[size] = { 0 };

    for (int i = 0; i < size; ++i)
        cin >> a[i];

    const int w = 4;
    cout << setw(w) << "[";
    for (int i = 0; i < size; ++i)
        cout << setw(w) << a[i];
    cout << setw(w) << "]" << endl;

    return 0;
}
...yields the following upon execution:
Code:
3 8 1 7 [ 3 8 1 7 ]
Some might question whether initialization is necessary given that each array element is overwritten.
  #7  
Old 22-Nov-2008, 00:35
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: input sorting program


Since you didn't post the whole program, we can only guess. Are you outputting int_values? If so, look again at the code you posted.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #8  
Old 22-Nov-2008, 00:36
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: input sorting program


Here is the full code. I just figured i would only need to show the bit that was changed.

CPP / C++ / C Code:
include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "iostream"
#include "windows.h"

using namespace std ;
int compare_int(int *a, int *b){

  if (*a < *b)
	return(-1) ;
  else if (*a == *b)
	return(0) ;
  else
	return(1) ;
}

int main(){

  int int_values[11] = {0} ;  
   
  int elements = 10 ;
  const int size = 10;
  int a[size] = { 0 };
  int i = 0 ;

  
  cout << "Please enter 10 numbers " ;
  cout << "in any order(separated" ;
  cout << " with spaces).\n" ;
  for (; i < size; ++i){
    cin >> a[i] ;
  }

  qsort(int_values, elements, 
	sizeof(int), (int (*) 
	(const void *, const void *))
	compare_int) ;

  for (i = 0; i < elements; i++){
    cout << int_values[i] << " " ;
  }

  cout << "\n" ;
  system ("pause") ;
  return 0 ;
}
  #9  
Old 22-Nov-2008, 00:45
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: input sorting program


Quote:
Originally Posted by genesaika
The input is set to zero for all of my array's elements
Note that you are reading values into array a, but sorting array int_values where all elements have been initialized to zero. What you are getting as output is exactly what you have specified in the code.
  #10  
Old 22-Nov-2008, 00:49
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: input sorting program


Doh why didn't I see that ? lol. Thank you
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
floating point decimal to ascii conversion crazypal C Programming Language 5 18-Apr-2007 05:59
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
Sorting program using pointer notation NEED HELP!! cougar1112 C++ Forum 16 14-Nov-2005 12:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.