Thread: A little help with pointers and arrays

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    A little help with pointers and arrays

    I have to write functions that manipulates an array containing numbers (doubles).
    I have to use pointers for all arrays to do this.

    One function prints the array, the next one triples each element in the array, and the last one reverses the array.

    Heres my code so far-
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
     void tripleEach(double * array , int arraySize)
     {
         int i;
         
         for (i = 0; i < arraySize; i++)
         {
          *(array + i) = (*(array + i) * 3);   
          //printf("%lf ", *(array + i));
         } 
     }
    
    void reverse(double * array, int arraySize)
    {
      int i;
      int j = 0;
      
      for( i = arraySize/2; i >= 0; i--)
      {
       *(array + i) = *(array + j);    
       j++; 
      }
         
    }
     
     void printArray(double * array, int arraySize)
     {
          printf("\n");
          int i;
          double num;
          
          for(i = 0; i<arraySize; i++)
          {
            printf("%lf ", *(array+i));    
          }
              
    
     }
     
    int main()
    {
        int i;
        double * numArray;
        int size;
        double num;
    
     
        printf("Please enter a number of floating numbers to be entered:\n");
        scanf("%d", &size);
     
        /***   Enter a line of code to allocate memory for the array here ***/
       
       (double *)malloc(size * sizeof(double));
       
     
        for (i=0; i<size; i++)
        {
           scanf("%lf", &num);  //read in a double entered by user
           *(numArray+i) = num;
        }
     
        printArray(numArray, size);
     
        reverse(numArray, size);
        printArray(numArray, size);
     
        tripleEach(numArray, size);
        printArray(numArray, size);
     
        return 0;
    }
    My program crashes after I type in the numbers that will go in the array. It seems to be a problem with my print function, my tripleEach functions seems to work. I haven't tested my reverse function, but it doesn't seem to be working right, but I'm more concerned about the print function. Any help would be appreciated

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matt_570 View Post
    I have to write functions that manipulates an array containing numbers (doubles).
    I have to use pointers for all arrays to do this.

    One function prints the array, the next one triples each element in the array, and the last one reverses the array.

    Heres my code so far-
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
     void tripleEach(double * array , int arraySize)
     {
         int i;
         
         for (i = 0; i < arraySize; i++)
         {
          *(array + i) = (*(array + i) * 3);   
          //printf("%lf ", *(array + i));
         } 
     }
    
    void reverse(double * array, int arraySize)
    {
      int i;
      int j = 0;
      
      for( i = arraySize/2; i >= 0; i--)
      {
       *(array + i) = *(array + j);    
       j++; 
      }
         
    }
     
     void printArray(double * array, int arraySize)
     {
          printf("\n");
          int i;
          double num;
          
          for(i = 0; i<arraySize; i++)
          {
            printf("%lf ", *(array+i));    
          }
              
    
     }
     
    int main()
    {
        int i;
        double * numArray;
        int size;
        double num;
    
     
        printf("Please enter a number of floating numbers to be entered:\n");
        scanf("%d", &size);
     
        /***   Enter a line of code to allocate memory for the array here ***/
       
       (double *)malloc(size * sizeof(double));
       
     
        for (i=0; i<size; i++)
        {
           scanf("%lf", &num);  //read in a double entered by user
           *(numArray+i) = num;
        }
     
        printArray(numArray, size);
     
        reverse(numArray, size);
        printArray(numArray, size);
     
        tripleEach(numArray, size);
        printArray(numArray, size);
     
        return 0;
    }
    My program crashes after I type in the numbers that will go in the array. It seems to be a problem with my print function, my tripleEach functions seems to work. I haven't tested my reverse function, but it doesn't seem to be working right, but I'm more concerned about the print function. Any help would be appreciated
    What you're trying to do in the highlighted line? malloc returns a pointer and you're not even assigning the returned value to anything.
    Also the cast to malloc is redundant.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    numArray isn't initialized with anything, so naturally it crashes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    it isnt initialized to point to any address so it will crash btw why arent you using indexing to multiplay values since your loop goes by index not by address you should just arr[i]*=3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM