|
 |
|
 |
10-26-2005, 06:07 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 6
|
Array help
Hey guys,
I need help with 2 programs. The first one compiles and runs but doesn't print the results. The code is below.
Code:
#include <stdio.h>
main()
{
char month[12][4];
int product[12];
float sales[12];
short i;
for (i = 1; i <= 12; i++) {
printf("\nEnter the month: ");
scanf("%s",month[i-1]);
printf("\nEnter number of products sold: ");
scanf("%d", &product[i - 1]);
printf("\nEnter sales: ");
scanf("%.2f", &sales[i - 1]);
}
return(0);
}
The second one I need a user to input 10 numbers and then show them a menu with 2 options, to sort in ascending or descending order. I really need help.
Code:
#include <stdio.h>
int Number[10];
int count;
main()
{
for(count=1;count<11;count++)
{
printf("Please enter a number: ");
scanf("%d", &Number[count]);
}
for (count=1;count<11;count++)
{
thanks a bunch
SweetOne
Last edited by redhead; 10-26-2005 at 06:14 AM.
Reason: added code tags
|
|
|
10-26-2005, 06:29 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
For the first one don't you just need to add a second loop to print the contents of the arrays month, product, and sales?
Code:
for (j = 0; j < 12; j++) {
printf("Month %s : Products: %d : Sales: %d", month[j], product[j], sales[j]);
}
__________________
Stop intellectual property from infringing on me
|
|
|
10-26-2005, 06:36 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 6
|
Here is my new code for the first one
PHP Code:
#include <stdio.h>
main()
{
char month[12][4];
int product[12];
float sales[12];
short i;
for (i = 1; i <= 12; i++) {
printf("\nEnter the month: ");
scanf("%s",month[i-1]);
printf("\nEnter number of products sold: ");
scanf("%d", &product[i - 1]);
printf("\nEnter sales: ");
scanf("%f", &sales[i - 1]);
}
for (i = 1; i <= 12; i++) {
printf("\nMonth:%s ",month[i-1]);
printf("\nNumber of products sold:%d ",product[i-1]);
printf("\nSales:%.2f ",sales[i-1]);
}
return(0);
}
Never mind..I had a .2 in my scan float for sales. I fixed that and it works like a charm. Now I need help with my second programming code from above 
|
|
|
10-26-2005, 07:29 AM
|
#4 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Have you dealt with sorting algorithms before? If not here's the basics of a bubblesort http://www.cs.princeton.edu/~ah/alg_...ubbleSort.html . Basically the idea is that you compare each element to every other element, until they're in order.
You've got the input loop done already, so you just need to write the sort function, and then make two ouput loops, one to print the numbers in acending order (by printing the sorted array in the forward direction) and one to print the array backwards, therefore giving you the descending order.
__________________
Stop intellectual property from infringing on me
|
|
|
10-26-2005, 07:34 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
For the second program:
take a look at this:
Code:
#include <stdio.h>
#define NUMBERS 10
int numbers[NUMBERS];
int sort(int ascending);
int main()
{
int i;
char ch;
while(1){
printf("Please select sort order the array\n");
printf(" 1) Fill in array\n");
printf(" 2) sort Ascending\n");
printf(" 3) sort DEscending\n");
printf(" q) quit\n");
printf("Selection: ");
fflush(stdout);
ch = getchar();
if(ch == 'q' || ch == 'Q')
break; /* we quit from here */
switch(ch)
{
case '1':
for (i=0; i < NUMBERS; ++i) {
printf("\nEnter number %d: ", i+1);
scanf("%d", &numbers[i]);
}
ch=getchar();
break;
case '2':
sort(1);
break;
case '3':
sort(0);
break;
default:
printf("Unsupported choice: %c\n", ch);
}
for(i=0; i < NUMBERS; ++i)
printf("%d%c ", numbers[i], (i+1)%NUMBERS?',':'\n');
printf("\n");
}
return(0);
}
int sort(int ascending)
{
int i, j, tmp;
for(i=NUMBERS-2; i >=0; i--)
{
tmp=numbers[i];
if(ascending)
for(j=i+1; j<=NUMBERS-1 && tmp > numbers[j]; ++j)
numbers[j-1]=numbers[j];
else
for(j=i+1; j<=NUMBERS-1 && tmp < numbers[j]; ++j)
numbers[j-1]=numbers[j];
numbers[j-1] = tmp;
}
return 0;
}
Bubblesort might be a bit too advanced at this point, so mine uses a lineary sort algorithm..
*Cough* the worst kind..
|
|
|
10-26-2005, 08:54 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 6
|
Thank you redhead. I actually understood that fairly well. I think I'll look into Bubblesort too. I'm new to C programming so I'm just learning. Thank you very much for your help.
SweetOne
|
|
|
10-26-2005, 09:31 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
The bubblesort algorithm is almost the same as my linear, if you want to upgrade, you can study the merge sorting algorithm or for the realy advanced level quick sort Or you might want to see all the different sorting algorithms.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 12:36 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|