Thread: array Challenge

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Lightbulb array Challenge

    I've written a program to take inputed integers from the keyboard and the use pointer to arrange the numbers in ascending and descending order without changeing the orignal entered numbers and then place into a table.

    For the most part I think I have it but am getting to errors and have not been able to decipher my errors could someone please help?

    I will attach code if someone is up to this challenge.

    Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    
    // Function Declaration
    int* getData (int* pAry, int arySize);
    void ascendSort (int* pAry, int* pCount);
    void descendSort (int* pAry, int* pCount);
    void printData (int* pAry, int* pCount);
    
    int main (void)
    {
    	//Local Declarations
    	int ary[SIZE];
    	int* pCount;
    
    	//Statements
    	pCount = getData (ary, SIZE);
    	ascendSort (ary, pCount);
    	descendSort (ary, pCount);
    	printData (ary, pCount);
    	
    	system ("PAUSE");
    
    	return 0;
    
    }// main
    /*=============================getData=========================================
    	Reads data input by user from keyboard for sorting purposes.
    		Pre pAry is pointer to array to be filled & arySize is the last #
    			in array.
    		Post array filled. returns address of last element.
    */
    int* getData(int* pAry, int arySize)
    {
    	//Local Declarations
    		int ioResult;
    		int* pFillAry = pAry;
    		int elementCnt;
    
    	//Statements
    		printf ("Please enter 5 integers: ");
    			do
    			{
    				ioResult = scanf_s ("&#37;d", pFillAry);
    				if (ioResult == 1)
    					{
    						pFillAry++;
    						elementCnt++;
    					}// if
    			} while (ioResult == 1 && elementCnt < arySize);
    			printf ("\n\n %d # of elements read.", elementCnt);
    		    return (--pFillAry);
    }//getData
    /*============================ascendSort=======================================
    	Sort Array in ascending order.
    		Pre pointer to array
    		Post sorted in ascending order
    */
    void ascendSort (int* pAry, int* pCount)
    {
    	//Local Declarations
    		int* pIndex;
    
    	//Statements
    		for (pIndex = pAry; pIndex < pCount; pIndex++)
    			
    		return;
    }//ascendSort
    /*==========================descendSort========================================
    	Sort Array in descending order.
    		Pre pointer to array
    		Post sorted in descending order
    */
    void descendSort (int* pAry, int* pCount)
    {
    	//Local Declarations
    	    int* pIndex;
    
    	//Statements
    		for (pIndex = pAry; pIndex < pCount; pIndex++)
    		
    		return;
    }//descendSort
    /*======================printData==============================================
    	Given a pointer to array. Print the data.
    		Pre pAry points to filled array.
    		    pCount identifies last element in array.
    		Post Prints data in table form
    */
    void printData (int* pAry, int* pCount)
    {
    	//Local Declarations
    		int numCt;
    		int* pTable;
    		
    	//Staements
    		printf ("\n\n Table \n\n");
    		printf("===================\n");
    		printf ("Asecnding Order\t Original entry\t DescendingOrder\n\n");
    		for (numCt = 0; numCt < 5; numCt++)
    			pTable[numCt] = pAry + count;
    		printf ("%d\t | %d\t | %d\t", ascendSort, pAry, descendSort);
    
    		return;
    }//printData
    //===============================End of Program================================
    Last edited by Salem; 12-08-2007 at 12:57 AM. Reason: Fixed code tags.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by silhoutte75 View Post
    I've written a program to take inputed integers from the keyboard and the use pointer to arrange the numbers in ascending and descending order without changeing the orignal entered numbers and then place into a table.

    For the most part I think I have it but am getting to errors and have not been able to decipher my errors could someone please help?

    I will attach code if someone is up to this challenge.

    Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    
    // Function Declaration
    int* getData (int* pAry, int arySize);
    void ascendSort (int* pAry, int* pCount);
    void descendSort (int* pAry, int* pCount);
    void printData (int* pAry, int* pCount);
    
    int main (void)
    {
    	//Local Declarations
    	int ary[SIZE];
    	int* pCount;
    
    	//Statements
    	pCount = getData (ary, SIZE);
    	ascendSort (ary, pCount);
    	descendSort (ary, pCount);
    	printData (ary, pCount);
    	
    	system ("PAUSE");
    
    	return 0;
    
    }// main
    /*=============================getData=========================================
    	Reads data input by user from keyboard for sorting purposes.
    		Pre pAry is pointer to array to be filled & arySize is the last #
    			in array.
    		Post array filled. returns address of last element.
    */
    int* getData(int* pAry, int arySize)
    {
    	//Local Declarations
    		int ioResult;
    		int* pFillAry = pAry;
    		int elementCnt;
    
    	//Statements
    		printf ("Please enter 5 integers: ");
    			do
    			{
    				ioResult = scanf_s ("%d", pFillAry);
    				if (ioResult == 1)
    					{
    						pFillAry++;
    						elementCnt++;
    					}// if
    			} while (ioResult == 1 && elementCnt < arySize);
    			printf ("\n\n %d # of elements read.", elementCnt);
    		    return (--pFillAry);
    }//getData
    /*============================ascendSort=======================================
    	Sort Array in ascending order.
    		Pre pointer to array
    		Post sorted in ascending order
    */
    void ascendSort (int* pAry, int* pCount)
    {
    	//Local Declarations
    		int* pIndex;
    
    	//Statements
    		for (pIndex = pAry; pIndex < pCount; pIndex++)
    			
    		return;
    }//ascendSort
    /*==========================descendSort========================================
    	Sort Array in descending order.
    		Pre pointer to array
    		Post sorted in descending order
    */
    void descendSort (int* pAry, int* pCount)
    {
    	//Local Declarations
    	    int* pIndex;
    
    	//Statements
    		for (pIndex = pAry; pIndex < pCount; pIndex++)
    		
    		return;
    }//descendSort
    /*======================printData==============================================
    	Given a pointer to array. Print the data.
    		Pre pAry points to filled array.
    		    pCount identifies last element in array.
    		Post Prints data in table form
    */
    void printData (int* pAry, int* pCount)
    {
    	//Local Declarations
    		int numCt;
    		int* pTable;
    		
    	//Staements
    		printf ("\n\n Table \n\n");
    		printf("===================\n");
    		printf ("Asecnding Order\t Original entry\t DescendingOrder\n\n");
    		for (numCt = 0; numCt < 5; numCt++)
    			pTable[numCt] = pAry + count;
    		printf ("%d\t | %d\t | %d\t", ascendSort, pAry, descendSort);
    
    		return;
    }//printData
    //===============================End of Program================================
    in getData: elementCnt is not initialized
    ascendSort, descendSort: um. um. These functions don't actually do anything. You do realize that?
    printData: the variable count does not exist. I'm curious as to why you think this should work. ascendSort and descendSort aren't integers, they're function pointers -- and even if you called them, you still wouldn't get an integer, since they don't return anything.

    Even if you had working subroutines, you'd still be in a hole: your main program calls ascendSort and then descendSort, so you've lost ascendSort by this time.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Smile could you help.

    After analyzing my program on paper I realized that my ascending and descending program was not actually doing anything.

    However I have tried to remedy the problem and have become brain fried. Do you think you could help?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    When that happens, the best thing to do is to step away from your code for an hour or two.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Most of your program doesn't do anything like what it is supposed to do.
    Your "brain fried" feeling comes form the fact that you're forcing yourself to write code without having a clue what you need to do, or even having a basic understanding of how much of the C language works.

    Simply stop writing random bits of code and first work out how to solve the problem. You should solve the problem in your head completely before you even touch the keyboard. If you can't do that yet then you need more books, web-tutorials or other resource material etc.
    The first thing you need to work out is where are you going to store the ascending and descending sequences.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM