Array loop

Discussion in 'C' started by xbxbxc, Apr 16, 2007.

  1. xbxbxc

    xbxbxc New Member

    Joined:
    Apr 16, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm trying to make a program that will ask the user to press y to continue. If y is pressed it will then ask for a number. It will multiply the entered number by each value in an array. I can get it to work fine the first time through the array; however, when I call for the function I defined to ask the user to continue the second time it will not work properly.

    Code:
    /*
    	This program is for practicing "array".
    	4/ 10/ 07
    */
    #include <stdio.h>
    #include <conio.h>
    #define SIZE 11
    
    void askContinue (void);
    void askForNumber (void);
    void sCalc (void);
    
    	float num;
    
    
    	int a [SIZE] = {2, 8, -7, 9, 0, 11, 14, 7, 12, 3, 5};
    
    
    int main ()
    {
    	printf ("This program has an array of size 11 which is initialized.\nYou may enter a number which\n");
    	printf ("will be multiplied to every value in the array.\n\n");
    	printf ("Initialized to: 2, 8, -7, 9, 0, 11, 14, 7, 12,3, 5");
    
    	askContinue ();
    
    }//main
    void askContinue ()
    {
    	char answer;
    
    	printf ("\nIf you want to continue press y");
    	printf ("\nOtherwise hit anyother key to exit\n\n");
    	scanf ("%c\n", &answer);
    
    	switch (answer) {
    		case    'y' :
    			askForNumber ();
    
    		break;
    
    		default :
    			printf ("Goodbye!");
    			getch ();
    			printf (&answer);
    			return 0;
    		}//switch
    
    } //askContinue
    void askForNumber ()
    {
    	printf ("\n Enter a number to be multiplied\n");
    	scanf ("\n%f\n", &num);
    	printf ("\n\nYou entered the number: %f\n\n", num );
    	sCalc ();
    }//askForNumber
    void sCalc ()
    {
    printf ("The original values of the array were:\n\n");
    	printf ("%s%9s\n", "Index", "value");
    
    	for (int i = 0; i <SIZE;i++)
    {
    
    	printf ("%3d%9d", i, a[i]);
    	printf ("\n");
    }
    	printf (" \nThe new values of the array are:\n\n");
    	for (int i = 0; i <= SIZE-1; i++)
    		{
    			a [i] = a[i] * num;
    
    			printf ("%3d%9d", i, a[i]);
    			printf ("\n");
    		}
    
    
    
    askContinue ();
    }//sCalc
    
     
  2. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Will have to look through the code. Will reply in some time
     
  3. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    You can try to add a loop inside main function.

    I hope you can success.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice