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 10-26-2005, 06:07 AM   #1 (permalink)
SweetOne
Registered User
 
Join Date: Oct 2005
Posts: 6
SweetOne is on a distinguished road
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
SweetOne is offline   Reply With Quote
Old 10-26-2005, 06:29 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 10-26-2005, 06:36 AM   #3 (permalink)
SweetOne
Registered User
 
Join Date: Oct 2005
Posts: 6
SweetOne is on a distinguished road
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 (
1<= 12i++) {
    
printf("\nEnter the month: ");
    
scanf("%s",month[i-1]);

    
printf("\nEnter number of products sold: ");
    
scanf("%d", &product[1]);
    
printf("\nEnter sales: ");
    
scanf("%f", &sales[1]);
  } 
for (
1<= 12i++) {
    
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
SweetOne is offline   Reply With Quote
Old 10-26-2005, 07:29 AM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 10-26-2005, 07:34 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-26-2005, 08:54 AM   #6 (permalink)
SweetOne
Registered User
 
Join Date: Oct 2005
Posts: 6
SweetOne is on a distinguished road
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
SweetOne is offline   Reply With Quote
Old 10-26-2005, 09:31 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using vars to create an array?? phill2000star PHP 5 07-17-2005 01:36 AM
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 06:57 PM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Text file input to an array letsC Standard C, C++ 9 03-06-2005 09:30 AM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM


All times are GMT -8. The time now is 12:36 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