1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Array loop

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

  1. xbxbxc

    xbxbxc New Member

    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.

    	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 ();
    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 ();
    		default :
    			printf ("Goodbye!");
    			getch ();
    			printf (&answer);
    			return 0;
    } //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 ();
    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 ();
  2. wrecker

    wrecker New Member

    Will have to look through the code. Will reply in some time
  3. Peter_APIIT

    Peter_APIIT New Member

    You can try to add a loop inside main function.

    I hope you can success.

Share This Page