Thread: Array and other problems

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Array and other problems

    im making a highest number program with counter and a multiplication program with array and im not sure where im going wrong, the highest number always comes out at 0, and in the multiplication the number always gets * by 12 (the ending number), thanks for any help on this problem! and sorry its a bit of a mess lol

    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    int main ()
    {
    	int choice = 0;
    	int multi = 0;
    	int x = 0;
    	int x1 = 0;
    	int x2 = 0;
    	int highest = 0;
    	int num1 = 0;
    	int num2 = 0;
    	int total = -1;
    
    	printf("Math Menu\n\n");
    	printf("\n1:Highest Number\n");
    	printf("2:Muliplication Table\n");
    	printf("\nPlease select (1 or 2):\t");
    	scanf("%c",&choice);
    	system ("cls");
    
    	switch (choice)
    	{
    	case '1':
    		printf("Highest Number Calc\n\n");
    		
    		do{
    		printf("\nPlease Input a number or input -999 to finish\n");
    		int highest(int, int);
    		scanf("%d", &num1);
    		
    		total = total + 1;
    
    		}while (num1 != -999);
    
    	printf("%d, is the highest number and there where %d, numbers inputed", highest, total);
    		break;
    	int highest(int num1, int num2);
    	{
    		if (num1 < num2)
    			return (num2);
    		if (num1 > num2)
    			return (num1);
    	}
    
    	case '2':
    		printf("Please Input a Number for your Multiplication Table: ");
    		scanf("%d", &x1);
    	int multiray[12];		
    	for(x = 0;x < 13; x++)
    		multiray[x] = x;
    	   (x2 = x*x1);
    	for(x = 0;x < 13; x++)
    		printf("%d x %d = %d\n", multiray[x], x1, x2);
    	    
    		break;
    
    
    	default:				
    		printf("\nInvalid selection\n");
    	}						// end switch
    	
    	getch();
        return 0;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    int highest(int, int);
    Here you're declaring a prototype for a function, NOT calling a function. You should put this above main, then take your function definition OUTSIDE of the switch statement and put it below main.

    Total should be placed at 0, not -1, I think.

    Code:
    for(x = 0;x < 13; x++)
    Your test condition must be x < 12, since 12 is the size of the array and 11 is the highest valid index.

    I suspect your problems stem from these two, but there may also be some others since I just did a quick scan of the code.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What are you expecting the brackets to do here?:
    (x2 = x*x1);
    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"

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I should optimize your case statement for option 2 to build out your multiplication table as such:

    Code:
    for(x = 0; x < 13; x++)
    {
    	multiray[x] = x * x1;
            printf("%d x %d = %d\n", x, x1, multiray[x]);
    }
        break;
    You should always limit your need for for-loops and variables when possible.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacNilly View Post
    Code:
    int highest(int, int);
    Here you're declaring a prototype for a function, NOT calling a function. You should put this above main, then take your function definition OUTSIDE of the switch statement and put it below main.
    Ah, not only that, but the OP is also defining a new function inside a function.
    The entire function should be moved outside main and the ; after the parameter list should be removed.
    And also to add to what MacNilly mentioned, add the names of the parameters back into that prototype.
    http://apps.sourceforge.net/mediawik...arameter_names
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed