4 Replies - 3083 Views - Last Post: 25 October 2012 - 07:54 AM Rate Topic: -----

#1 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Storing numbers in an array and printing them

Posted 24 October 2012 - 03:47 PM

Question: Write a program that displays all the odd numbers and all the even numbers from one to twenty.
First my answer was as follows:
#include<stdio.h>
 
main()
{
   int i;
 
  printf("The following are all odd numbers between 1 and 20 :\n");
   for ( i=1; i <= 20; i++ ){
		if (i%2)
			printf ("%d ",i);
	}	
  printf("\nThe following are all even numbers between 1 and 20 :\n");
   for ( i=1; i <= 20; i++ ){
		if (!(i%2))
			printf ("%d ",i);
     	 }
getch();
}


This one worked.
For the sake of practice I decided to do it differently using arrays and I wrote the following code:
#include<stdio.h>
 
main()
{
   int  o=0, e=0, i, a, t, odd[10], even[10];
   
   for ( i=1; i <= 10; i++ ){
		if (i%2){
			odd[o]=i;
			o++;
		}
		else {
			even[e]=i;
			e++;
		}
	}	   
 
    printf("The following are all odd numbers between 1 and 20 :\n");
   for ( a=0; a <= 9; a++ ){
			printf (" %d,", odd[a]);
	}	
	
  printf("\n\nThe following are all even numbers between 1 and 20 :\n");
   for ( t=0; t <= 9; t++ ){
			printf (" %d,", even[t]);
	}	

getch();
}


This code only works up to 10 and starts to misbehave thereafter. What did I mess up?

This post has been edited by murume: 24 October 2012 - 03:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing numbers in an array and printing them

#2 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,110
  • Joined: 25-December 09

Re: Storing numbers in an array and printing them

Posted 24 October 2012 - 04:04 PM

Quote

This code only works up to 10 and starts to misbehave thereafter. What did I mess up?

In the following line.
for ( i=1; i <= 10; i++ ){


In C and C++ arrays start at zero, not one, and the stop at size - 1, not size. You need to get used to this fact, "always" start your for loops at zero and don't use the operator<= or operator>=.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,033
  • Joined: 18-April 07

Re: Storing numbers in an array and printing them

Posted 24 October 2012 - 04:08 PM

Also keep in mind that if your for loop is only going up to 10, how are you going to ever get to 20? Try setting up the for loop to be i = 0; i < 20;.

Your for loop goes through all 20 numbers, your if statement is going to decide which array the value goes into.

:)
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,110
  • Joined: 25-December 09

Re: Storing numbers in an array and printing them

Posted 24 October 2012 - 04:15 PM

I actually recommend you use a const variable to set the size of your array, and for use in your for loops.

#include<stdio.h>
 
#define Size 10

int main()
{
   int  o=0, e=0, i, a, t, odd[Size], even[Size];
   
   for ( i=0; i < Size; i++ ){
		if (i%2){
			odd[o]=i;
			o++;
		}
		else {
			even[e]=i;
			e++;
		}
	}	   


Then you only need to change one thing to allow different sized arrays, and loops.

Also note main() should be defined to return an int and you should return an int from this function. Although default arguments are allowed with the C90 standard, the current standard no longer allows default arguments.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Re: Storing numbers in an array and printing them

Posted 25 October 2012 - 07:54 AM

Thank you guys. Now I'm going to submit the one with arrays because it looks clever.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1