Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-07-2004, 07:00 PM   #1 (permalink)
somnath
Registered User
 
Join Date: Oct 2004
Posts: 4
somnath is on a distinguished road
need help

Write a complete program that asks user to input 12 numbers of any types or domain (i.e. positive or negative) and record user's response into, an array.

Process the input by manipulating it so that the numbers are arranged in the array in an ascending order (e.g. -1.2, -1, 0, 3, 4, 4.05, 4.1, 5).

After the manipulation is complete, print the resulting values in the array in one line.


-----
here is what i got



#include <iostream>
using namespace std;

int main(void)
{
double Numbers[12];

cout<<"Enter 12 numbers: ";

for ( int x = 0; x < 12; x++ )
cin>> Numbers;

cout<<endl;
}

i know i gotta do something like "x + 1 == 12" or something...but duno what
hope u guys can help me out
somnath is offline   Reply With Quote
Old 11-08-2004, 07:39 AM   #2 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Are you supposed to sort after all data is read, or AS you read it in?
The first is much, much easier than the second.
kaeli69 is offline   Reply With Quote
Old 11-08-2004, 08:07 AM   #3 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Here's an example of the first - a simple bubble sort after the fact.
Note that for testing, I made the number of inputs to read as a constant (so you can just do 5 for testing, then up it to 12).
Also note I haven't done anything with the rounding - it will print the full double.

Code:
#include <stdio.h>

#define NUM 5

void printArray(double a[])
{
   int x=0;
   printf("\nYou entered:\n");
	for (x=0; x<NUM; x++)
	{
      printf("%lf ", a[x]);
	}
   printf("\n");
}

void sortArray(double a[])
{
  int i,j; 
  double tmp; 
  
  for (i=0; i<NUM; i++)
     {
     for (j=NUM-1; j>i; j--) 
        {
        if (a[j] < a[j-1]) 
           {
           tmp = a[j];
           a[j] = a[j-1];
           a[j-1] = tmp;
           }
        }
     }
}

int main(int argc, char *argv[])
{
   double numbers[NUM];
   double i;
   int x;

	printf("Please enter %d numbers.\n", NUM);
	for (x=0; x<NUM; x++)
	{
      scanf("%lf", &i);
      numbers[x] = i;
	}
   
   sortArray(numbers);
   printArray(numbers);
	return 0;
}
Sample output from Unix terminal:
Unix >> cc testNumbers.c
Unix >> a.out
Please enter 5 numbers.
3
5
6
0
-12

You entered:
-12.000000 0.000000 3.000000 5.000000 6.000000
Unix >> cc testNumbers.c
Unix >> a.out
Please enter 5 numbers.
34
36.5
-23.6
12
5

You entered:
-23.600000 5.000000 12.000000 34.000000 36.500000
kaeli69 is offline   Reply With Quote
Old 11-08-2004, 10:58 AM   #4 (permalink)
somnath
Registered User
 
Join Date: Oct 2004
Posts: 4
somnath is on a distinguished road
kaeli, Thanks very much for help. But this is not exactly what im looking for. your program looks too hectic!! i need lil simple than that. but thaks i really appreciate you
somnath is offline   Reply With Quote
Old 11-08-2004, 04:28 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll attend the board tomorrow.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting