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 01-21-2005, 06:55 AM   #1 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
Post Simple reverse programm

Please consider this code
It has some bugs, which I don't know yet how to remove them (maybe I should try RAID :/)
Maybe there is another way to do this, I mean some more advanced methods.
If you have any questions - ask.

Code:
//*****reverse.c*****//
//A program getting integer array from keyb, reversing it and writing in another array//
//Author: silex //
//Date: beggining: 18.01.05 end: not yet end//


#include <stdio.h>



int main()  {
     
     int a_elem=0,                       
         i=0,                                         
         index=0,
         rev_index=0;                                    
     
       printf("\npass me total array elements: ");       
       scanf("%d", &a_elem);
     
       int i_array[100]={0},      
           rev_array[100]={0};
      
      for (i=0; i < a_elem; i++)   {
           printf("\nelement: ");
           scanf("%d ", &i_array[index]);
           index++;
           
        }
     index=a_elem;
     
     for (i=0; i< a_elem; i++)     {
           rev_array[rev_index]=i_array[index];
           index--;
           rev_index++;
             
        }
     printf("\n\t array: \n");
     for (i=0; i < a_elem; i++)    {
           printf("%d.  ", i_array[index]);
           index++;
        }
     
     
     printf("\n\n\t reversed array:  \n");
     index=0;
     for (i=0; i < a_elem; i++)    {
           printf("%d.  ", rev_array[index]);
           index++;
        }
           
           
  }
silex is offline   Reply With Quote
Old 01-21-2005, 07:44 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Well, I'm not sure what exactly the problem was. There are a few things I see though. You say you would like to see some 'advanced' techniques. Well, these may not be 'advanced' per-se, but they are a step in the right direction.

First, your array is a fixed size of 100, and you have no check to make sure that the user doesn't enter anything greater than 100! What if the user enters 110, and and after the 100th entry it crashes? A simple if statement can make that disappear:

PHP Code:
if (a_elem >= 100) { 
    
/* Do stuff */ 

Second, if you really don't care about actually 'reversing' the array and then printing it out, you can do it as follows:

PHP Code:
#include <stdio.h>
#include <stdlib.h> /* For malloc */

int main() {
   
unsigned int numElem/* Always a positive integer */
   
int *arr;
   
int i;

   
/* Get the Number of Elements */
   
printf ("Number of elements: ");
   
scanf("%i", &numElem);
   if (
numElem == 0) return 0;

   
/* Make the array */
   
arr = (int *) malloc (numElem sizeof(unsigned int));
   if (!
arr) {
      
printf ("Unable to make array!\n");
      return -
1;
   }     

   
/* Get each element */
   
for (0numElemi++) {
      
printf ("Element %d: "1);
      
scanf ("%d", &arr[i]);
   }

   
/* Print the array */
   
printf ("Array: ");
   for (
0numElemi++) {
      
printf ("%d "arr[i]);
   }
   
printf ("\n");

   
/* Print the 'reversed' array */
   
printf ("Reversed Array: ");
   for (
numElem 1>= 0i--) {
      
printf ("%d "arr[i]);
   }
   
printf ("\n");

   
/* Delete the array */
   
free (arr);

   return 
0;

This is sort of cheating though. This might not be what you want to do if this is a homework assignment to reverse an array.

The program above has some benefits though. First, it uses dynamic array allocation to create the array (100). This puts no stipulations on how many entries the user can enter. The only boundary in this case is the size of the unsigned int (which is more than you'll ever have to deal with, or want to type in by hand ). Second, it uses an unsigned int to make sure the user doesn't enter a negative number. (A negative number in your program would have weird effects).

If the assignment really is to reverse the array, and then print it out (as your comments suggest) you could used a data structure called a Stack to do the reversal for you. I can whip up some code to do this if you would like. I do not post it here since you are using C, and I'd have to do the stack code as well.

The concept behind using a stack (if you understand what a stack is you can skip this paragraph) is that since a stack is a LIFO data structure (Last in First Out: What you put on first will be the last one to come back out, like a stack of plates at a cafeteria) you push all the elements onto a a stack as they come from the user (or put them in an array and then onto the stack) and read them back out of the stack into another array (or same array). The mere process of doing this reverses the array.

If the problem really is for you to reverse an array. I wonder if you can do it in the same array or not. For instance, if you can reverse the array 'in place', you can use the following code to reverse the array:

PHP Code:
/* Please not that numElem is the number of elements in the array, and arr is the array of type int */

for (0numElem 2i++) {
    
int temp arr[i];
    
arr[i] = arr[numElem 1];
    
arr[numElem 1] = temp;

This is useful because it cuts the amount of steps to reverse the array in half.

If you really do need to create two arrays: one being one way and then reversing it into another, you don't really have a choice but to do it the long, hard way:

PHP Code:
/* Again, numElem is number of elements, arr is the array of type int, and revArr is the array we want to reverse into*/

for (int i 0numElemi++) {
    
revArr[numElem i] = arr[i];

I guess the most important thing you could take from this to incorporate into your own program is the dynamic allocation, and maybe that last bit. But now that I have written far more than you ever wanted to know, I'll leave you to ponder it . Good luck!

-Ted

PS: I am sorry if this either didn't make sense or if I explained stuff too much. Just let me know!
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 01-22-2005, 06:27 AM   #3 (permalink)
silex
Registered User
 
silex's Avatar
 
Join Date: Jan 2005
Posts: 10
silex is on a distinguished road
Hey ender.

At the beginning I would like to thank for this huge post.

When I compiled my program the error of witch I spoke before was following:
The array (non reversed) was printed good: 1. 2. 3.
But... The reversed array appeared like that: 0. 3. 2.
For 3 element array.
The second error, or bug maybe, was very weird for me:
when the putting into array code appeared:
Code:
for (i=0; i < a_elem; i++)   {
           printf("\nelement: ");
           scanf("%d ", &i_array[index]);
           index++;
}
The process appeared like that:

Element: 1 //pressed enter ??
2 //pressed enter ?? this is the weird thing
Element: 3 //pressed enter etc...


For the first thing I think I got an explanation: The reversed array starts looks like that:
0 1
1 2
2 3
3 4
4 0 // unassigned, but the reversed array starts from here

I don't know what is the problem for the second thing.


Could you explain the following code (I don't understand how the malloc() works) :
Code:
/* Make the array */
   arr = (int *) //esspecialy this part// malloc (numElem * sizeof(unsigned int));
   if (!arr) {
      printf ("Unable to make array!\n");
      return -1;
Dont worry you can explain too much, or too much, or anything else.
I am open for any amount of knowledge (Thats the cause I'm writing and asking)

Thanks again for help,

Jack
silex is offline   Reply With Quote
Old 01-22-2005, 09:03 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
You're off by one:
Code:
      for (i=0; i < a_elem; i++)   {
           printf("\nelement: ");
           scanf("%d ", &i_array[index]);
           index++;           
        }
     index=a_elem;
When exiting this loop your index is pointing at the NULL terminating your initial array.
So when you reverse:
Code:
     
     for (i=0; i< a_elem; i++)     {
           rev_array[rev_index]=i_array[index];
           index--;
           rev_index++;             
        }
Your initial value will be an int representing NULL as the first value in the reversed array. And since you're testing i against the array size, you'll miss the first value in your initial array.
__________________
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
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
Simple C program Spooky Standard C, C++ 1 10-22-2004 08:26 AM
Can't do a simple flippin' update . . . metazai PHP 10 06-04-2004 12:50 PM
Discussion board of your dreams - Simple Machnies Forums Phoenix PHP 0 08-10-2003 03:59 PM


All times are GMT -8. The time now is 08:37 AM.


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