Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Array loop (http://www.go4expert.com/forums/array-loop-t3888/)

xbxbxc 16Apr2007 08:37

Array loop
 
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


wrecker 17Apr2007 11:06

Re: Array loop
 
Will have to look through the code. Will reply in some time

Peter_APIIT 17Apr2007 11:24

Re: Array loop
 
You can try to add a loop inside main function.

I hope you can success.


All times are GMT +5.5. The time now is 12:37.