Thread: 2D Array

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    2D Array

    How do i save something to a 2D array and check for a previous occurence of it?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To put something in a 2D array:

    array[x_value][y_value] = some_value;

    To compare it, you want to loop through each colum and row, looking for duplicates.

    If you know how to loop through a single dimension array, you should be able to do the same for a double without much trouble.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Could you specifically show me where in here it goes cause I'm stupid or something and it's stumped me for a week or so now.

    Code:
    #include <stdio.h> 
    
    /* This function takes all the characters and makes them UPPERCASE */
    int LowUpp(int ch)
    {
    	if ('a' <= ch && ch <= 'z')
    	{
    		return (ch + 'A' - 'a');
    	}
    	else
    	{
    		return ch;
    	}
    }
    
    char c; /* Initializes c */
    
    int main()
    {
    	
    	int LowUpp(int ch);
    
    	/* The following delcares the variables to be used in the program */
    	char word[23];
    	char userstring[1024];
    	char Array[1024][100];
    	int wordcount = 0;
    	int charcount = 0;
    	int wordnumb = 0;
    	int i = 0;
    	int j = 0;
    	int row = 0;
    	int column = 0;
    
    	/* Next the Title and Enter your input are printed to the screen */
    	printf("Word & Character Counter\n\n\n");
    	printf("Enter your input ([CTRL+Z] To Quit): ");
    
    	/* This if statement takes in the string from the user */
    	if(fgets(userstring, 1024, stdin) == NULL)
    		return 0;
    
    	/* This prints the main parts of the table */
    	printf("\n\nWord Count		");
    	printf("Word			");
    	printf("Character Count	\n");
    	printf("---------------------------------------------------------------\n");
    
    	/* This while loop continues as long as the userstring[i] is not null */
    	while(userstring[i] != '\0')
    	{ 
    		/* If the userstring is equal to a space AND return AND tab */
    		if((userstring[i] != ' ') && (userstring[i] != '\n') && (userstring[i] != '\t'))
    		{ 
    			/* Increments charcount */
    			charcount++;
    
    			/* Uses the LowUpp function on the input whichs turns in into uppercase */
    			word[j++] = LowUpp(userstring[i]);
    		}
    		else
    		{
    			/* Increments wordnumb */
    			wordnumb++;
    
    			word[j] = '\0';
    
    			/* Saves it to the array */
    			Array[row][column] = word[i];
    
    			/* If charcount is less than 8 */
    			if(charcount < 8)
    				/* Prints the input to the screen */
    				printf("%d\t\t\t%s\t\t\t%d\n", wordnumb, word, charcount);
    			else
    				/* Prints the input to the screen */
    				printf("%d\t\t\t%s\t%d\n", wordnumb, word, charcount);
    
    			/* Sets charcount to 0 */
    			charcount = 0;
    
    			/* Increments wordcount  */
    			wordcount++;
    
    			/* Sets j equal to 0 */
    			j = 0;
    		}
    		
    		/* Increments i */
    		i++;
    	}
    	
    	/* Prints the end of the table to the screen */
    	printf("---------------------------------------------------------------\n");
    	printf("Total Words:  %d\n\n", wordcount);
    	
    	/* Returns 0 */
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM