Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,998 people online right now. Registration is fast and FREE... Join Now!




array help

2 Pages V  1 2 >  
Reply to this topicStart new topic

array help

mitchelltab
21 Sep, 2006 - 06:56 PM
Post #1

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
okI'm starting out small so please bear with me. This is what I have to do. C program that will accept up to 50 validated integers from the keyboard (a null entry terminates the input process), store the integers in a subscripted array, sort the array in ascending sequence, copy the sorted values to a linked list organized with pointers, and then display the values on the screen. so i have broken it into 6 step. So the first on i want to do is to input the intergerts into an array. I have lowed the number in the array to 5. So i need to know what i did wrong to get it into my array the first time

CODE
#include <stdio.h>

#define Max 5 //number to set array

main ()
{
     int Num[Max]; // numbers to be added to array
  
   printf ("please enter 5 random nunber");
   scanf ("%d", &Num);
  
   while (Num == NULL)
   {
     printf ("please enter 50 random nunber");
     scanf ("%d", Num);
     }
    printf ("%d",  Num);  

getchar();
return 0;    
}    

User is offlineProfile CardPM
+Quote Post

MaXiMuS
RE: Array Help
21 Sep, 2006 - 08:42 PM
Post #2

New D.I.C Head
*

Joined: 21 Sep, 2006
Posts: 8


My Contributions
Okay, here's the new code :

CODE
#include <stdio.h>

#define Max 5 //number to set array

main ()
{
   int Num[Max]; // numbers to be added to array
   int i;
  
   for (i = 0; i < Max && Num[i] != 0; i++)
   {
       printf ("Please enter a random nunber : ");
       scanf ("%d", &Num[i]);
   }
  
   for (i = 0; i < Max; i ++)
       printf ("%d\n",  Num[i]);

   getchar();
   return 0;    
}


What was wrong in YOUR code was that your code would JUST input the number into the FIRST element of the array :
CODE
scanf ("%d", &Num);

So you need a loop to input the number in EVERY element of the array, like in the new code above.

Also, when you use :
CODE
printf ("%d", Num);

it prints the address of the FIRST element in the memory (correct me if I'm wrong here, I'm a C++ person).

You again need a loop to output all the elements.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Array Help
22 Sep, 2006 - 06:07 AM
Post #3

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(MaXiMuS @ 21 Sep, 2006 - 09:42 PM) *

Okay, here's the new code :

CODE
#include <stdio.h>

#define Max 5 //number to set array

main ()
{
   int Num[Max]; // numbers to be added to array
   int i;
  
   for (i = 0; i < Max && Num[i] != 0; i++)
   {
       printf ("Please enter a random nunber : ");
       scanf ("%d", &Num[i]);
   }
  
   for (i = 0; i < Max; i ++)
       printf ("%d\n",  Num[i]);

   getchar();
   return 0;    
}


What was wrong in YOUR code was that your code would JUST input the number into the FIRST element of the array :
CODE
scanf ("%d", &Num);

So you need a loop to input the number in EVERY element of the array, like in the new code above.

Also, when you use :
CODE
printf ("%d", Num);

it prints the address of the FIRST element in the memory (correct me if I'm wrong here, I'm a C++ person).

You again need a loop to output all the elements.

where can i find information on subscripted array. I searched a couple of sites last night and in my bood and did not find anything
User is offlineProfile CardPM
+Quote Post

Antiokus
RE: Array Help
22 Sep, 2006 - 06:12 AM
Post #4

D.I.C Head
**

Joined: 6 Sep, 2006
Posts: 127



Thanked: 1 times
My Contributions
As maximus said, you need to use a loop to actually move positions within the array. The way you have it now, it is assigning only one element of the array. With the For loop, 'i' acts as both a counter as well as the place in the array.

As the loop runs, 'i' is increased by one ("i++") each time. when you scan in the number inside the loop you assign it to "num[i]". Say 'i' = 2, so when you run the loop, you will assign to "num[2]", which is actually the third place in an array. Array positions start with 0 being the first place.

One thing he didn't mention was that within the "while" loop you have there, on your scanf statement, you are missing a & before "num"

scanf ("%d", Num);
should be:
scanf ("%d", &Num);
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Array Help
22 Sep, 2006 - 06:17 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
QUOTE(mitchelltab @ 22 Sep, 2006 - 10:07 AM) *

[where can i find information on subscripted array. I searched a couple of sites last night and in my bood and did not find anything

The subscript operator is the [] operator...it simply refers to indexing an array.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Array Help
22 Sep, 2006 - 06:32 AM
Post #6

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(Amadeus @ 22 Sep, 2006 - 07:17 AM) *

QUOTE(mitchelltab @ 22 Sep, 2006 - 10:07 AM) *

[where can i find information on subscripted array. I searched a couple of sites last night and in my bood and did not find anything

The subscript operator is the [] operator...it simply refers to indexing an array.

so my next strp is going to be sort the array in ascending sequence. So i just need to write a funcation that will sort the numbers in ascending order.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Array Help
22 Sep, 2006 - 07:24 AM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You can find explainations and examples of different sort algorithms here.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Array Help
22 Sep, 2006 - 09:52 AM
Post #8

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
ok this is what I have come up with to do the progam listed above. Im getting the following error when i try to debug any ideas on what i did wrong.
E:\tmitchellwk3.cpp In function `int main()':
58 E:\tmitchellwk3.cpp invalid conversion from `int' to `int*'
58 E:\tmitchellwk3.cpp initializing argument 1 of `void ArraySort(int*, int (*)(int, int), unsigned int)'

CODE
#include <stdio.h>
#include <stdlib.h>

#define unit32 unsigned int
#define Max 5 //number to set array

typedef int (*CMPFUN)(int, int);

void ArraySort (int i[], CMPFUN fun_prt, unit32 ub)
{  //start bubble array
  
           unit32 indx;
           unit32 indx2;
           int temp;
           int temp2;
           int flipped;
          
           if (ub <= 1)
            return;
            
            indx =1;
            do
            {
                  flipped = 0;
                  for (indx2 = ub-1; indx2 >= indx; --indx2)
                  {
                      temp = i[indx2];
                      temp2 = i[indx2 -1];
                      if ((*fun_prt)(temp2,temp)>0)
                      {
                          i[indx2 -1] = temp;
                          i[indx2] = temp2;
                          flipped =1;
                       }
                  }
            }while ((++indx < ub) && flipped);
}

int cmpfun (int a, int b)
{
    if (a > b)
     return 1;
    else if (a < b)
      return -1;
     else
       return 0;
}                  
main ()
{
   int Num[Max]; // numbers to be added to array
   int i;
  
   for (i = 0; i < Max && Num[i] != 0; i++)
   {
       printf ("Please enter a random nunber : ");
       scanf ("%d", &Num[i]);
   }
  
   ArraySort(i, cmpfun, Max);

   for (i = 0; i < Max; i ++)
        printf ("%d\n",  Num[i]);

   getchar();
   getchar();
   return 0;    
}
  

User is offlineProfile CardPM
+Quote Post

Xing
RE: Array Help
22 Sep, 2006 - 03:43 PM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Modified Code
CODE
#include <stdio.h>
#include <stdlib.h>

#define unit32 unsigned int
#define Max 5 /*number to set array*/

typedef int (*CMPFUN)(int, int);

void ArraySort (int i[], CMPFUN fun_prt, unit32 ub)
{  /*start bubble array*/
  
           unit32 indx;
           unit32 indx2;
           int temp;
           int temp2;
           int flipped;
          
           if (ub <= 1)
            return;
            
            indx =1;
            do
            {
                  flipped = 0;
                  for (indx2 = ub-1; indx2 >= indx; --indx2)
                  {
                      temp = i[indx2];
                      temp2 = i[indx2 -1];
                      if ((*fun_prt)(temp2,temp)>0)
                      {
                          i[indx2 -1] = temp;
                          i[indx2] = temp2;
                          flipped =1;
                       }
                  }
            }while ((++indx < ub) && flipped);
}

int cmpfun (int a, int b)
{
    if (a > b)
     return 1;
    else if (a < b)
      return -1;
     else
       return 0;
}                  
int main ()
{
   int Num[Max]; /* numbers to be added to array*/
   int i;
  
   for (i = 0; i < Max && Num[i] != 0; i++)
   {
       printf ("Please enter a random nunber : ");
       scanf ("%d", &Num[i]);
   }
  
   ArraySort(Num, cmpfun, Max);

   for (i = 0; i < Max; i ++)
        printf ("%d\n",  Num[i]);

   getchar();
   getchar();
   return 0;    
}


This post has been edited by Xing: 22 Sep, 2006 - 03:48 PM
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Array Help
22 Sep, 2006 - 03:55 PM
Post #10

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
Lets say that i want to only add 20 munber by the max is 50. Would i use a while statement or something else to stop the loop
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Array Help
22 Sep, 2006 - 05:14 PM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You can use any type of loop...simply make the break condition the number you want to stop at.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Array Help
22 Sep, 2006 - 09:52 PM
Post #12

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Yes, True....

Also check out the Code Snippets Section if you want to experiment with Sort Algorithms.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/5/08 01:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month