Thread: problem in returning array to main.

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    1

    problem in returning array to main.

    Hi all,

    I am returning to programming after a long time.

    Code:
    #include <stdio.h>
    
    
    void test_armstrong(int number, int *array)
    {
        int a[4], sum,i, j, temp;
            
        temp = number;
        
        a[3]   =  number/1000;
        number =  number - (a[3]*1000);
        a[2]   =  number/100;
        number =  number - (a[2]*100);
        a[1]   =  number/10;        
        number =  number - (a[1]*10);
        a[0]   =  number;
        
        sum = 0;
           
        for (i = 0; i < 4 ; i++)
        {
            a[i] = a[i]*a[i]*a[i]*a[i];
            sum = sum + a[i];
        }
        
            if (sum == temp)
               {                  
                      *array = temp;
                      printf("Armstrong number %d\n", *array);
                      array++;
                }      
    }
    
    
    int main()
    {
        int i, j, number, start, end;
        int array[] = {NULL};
        
        start = 1000;
        end = 9999; 
        
        for( i = start; i <= end; i++)
        {         
                    number =  i;       
              test_armstrong(number, array);
        }        
        
        printf("\n\n%d\n", *array);
    }
    .
    problem in returning array to main.-capture_test-jpg

    I want the print of the array in the function and the print of array to be the same but I am only getting the last number of array when printing the array in the main function. Please help me. Thankyou.
    Attached Images Attached Images problem in returning array to main.-capture_test-jpg 
    Last edited by shomikc; 11-18-2021 at 10:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can't return arrays.
    Nor can you print a whole array using printf.
    Nor is an array inherently aware of it's capacity.

    Code:
    #include <stdio.h>
    
    void test_armstrong(int number, int *array, int *nResults)
    {
        int a[4], sum,i, j, temp;
            
        temp = number;
        
        a[3]   =  number/1000;
        number =  number - (a[3]*1000);
        a[2]   =  number/100;
        number =  number - (a[2]*100);
        a[1]   =  number/10;        
        number =  number - (a[1]*10);
        a[0]   =  number;
        
        sum = 0;
           
        for (i = 0; i < 4 ; i++)
        {
            a[i] = a[i]*a[i]*a[i]*a[i];
            sum = sum + a[i];
        }
        
            if (sum == temp)
               {                  
                      array[*nResults] = temp;
                      printf("Armstrong number %d\n", array[*nResults]);
                      (array[*nResults])++;
                }      
    }
    
    
    int main()
    {
        int i, j, number, start, end;
        int array[20] = {0};  // how many?
        int nResults = 0;
        
        start = 1000;
        end = 9999; 
        
        for( i = start; i <= end; i++)
        {         
                    number =  i;       
              test_armstrong(number, array, &nResults);
        }        
        
        printf("\n\n%d\n", nResults);
        // a for loop is left as an exercise to the reader.
    
        return 0;  // so you don't see something silly like return 7
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning char array from a function to main
    By gamers18 in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2012, 02:20 PM
  2. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  3. Problem returning char array!
    By boblettoj99 in forum C Programming
    Replies: 7
    Last Post: 12-19-2009, 10:40 AM
  4. Odd problem: returning array from function
    By disorder in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2008, 01:21 AM
  5. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 04:26 PM

Tags for this Thread