All pastes #1600623 Raw Edit

clueless

public text v1 · immutable
#1600623 ·published 2009-10-07 06:13 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <time.h>#define SIZE 2int seed(){        //this is to get some better values out of the randomize funktion        int i, stime;        long ltime;        /* get the current calendar time */        ltime = time(NULL);        stime = (unsigned) ltime/2;        srand(stime);}int menu(){        int choice =0;        while(choice <1 || choice > 6)        {                printf("\n\nMenu for lab4\n\n 1. Enter numbers into array\n 2. Print the arrays\n 3. Add the arrays\n 4. Transpose the arrays\n 5. Multiply the arrays\n\n 0.Exit the program\nYour choice:");                scanf("%d",&choice);                return choice;        }}int enterArray(int (*ptrOne)[SIZE], int(*ptrTwo)[SIZE]){        int choice = 0;        while(choice >3 || choice <1)        {                printf("\n\n Lets add some numbers to the array:\n 1. Manually\n 2. Let the computer randomize some numbers for us\nYour choice: ");                scanf("%d", &choice);        }        int i,j,k;        switch(choice)        {                case 1:                {			for(k=0; k<2; k++)			{	                        	for(i=0; i<SIZE; i++)                	        {					for(j=0; j<SIZE; j++)					{	                        	        printf("\nEnter number into array nr %d [%d][%d]: ",k,i,j);						if(k)						{        	        	                	scanf("%d",(*(ptrTwo + j)+i));						}						else						{							scanf("%d",(*(ptrOne + j)+i));						}					}						                        }			}                        break;                }                case 2:                {                        seed(); // seed the random function	        	for(k=0; k<SIZE; k++)			{		                for(i = 0; i<SIZE; i++)        	                {					for(j = 0; j<SIZE; j++)					{						if(k)	                        			*(*(ptrTwo + i)+j) = rand() % 10;						else							*(*(ptrOne + i)+j) = rand() % 10;					}        	                }			}                        break;                }        }        return 0;}int printArray(int (*ptrOne)[SIZE], int (*ptrTwo)[SIZE]){	//some counters	int i,j,k;	for(k=0; k<2; k++) // so that we know what array we are printing from	{		printf("\n\nArray number: %d\n",k+1); 		for(i = 0; i<SIZE; i++)		{			for(j = 0; j<SIZE; j++)			{				if(k)					printf("%5d ",*(*(ptrTwo +i)+j));				else					printf("%5d ",*(*(ptrOne + i) + j));							}			printf("\n");		}		}	return 0;}int addArray(int (*ptrOne)[SIZE], int (*ptrTwo)[SIZE], int (*ptrThree)[SIZE]){	//some counters	int i,j;		printf("If we add the two arrays we get: \n");	for(i = 0; i < SIZE; i++)	{		for(j=0; j < SIZE; j++)		{			*(*(ptrThree +i) + j) = *(*(ptrOne + i) + j) + *(*(ptrTwo + i) + j);			printf("%5d ",*(*(ptrThree + i ) + j));		}		printf("\n");	}	return 0;}int transcendArray(int (*ptrOne)[SIZE], int (*ptrTwo)[SIZE]){		//some counters	int i,j,k;	printf("\n This is the transcend of the arrays\n\n");	for(k=0; k<2; k++) // so that we know what array we are printing from	{		printf("\n\nArray number: %d\n",k+1); 		for(i = 0; i<SIZE; i++)		{			for(j = 0; j<SIZE; j++)			{				if(k)					printf("%5d ",*(*(ptrTwo +j)+i));				else					printf("%5d ",*(*(ptrOne + j) + i));							}			printf("\n");		}		}	return 0;}int multiplyArray(int (*ptrOne)[SIZE], int (*ptrTwo)[SIZE], int(*ptrThree)[SIZE]){	//some counters for the multplying we are about to do	int i,j,k;		for(i = 0; i < SIZE; i++)		{			for(j = 0; j < SIZE; j++)			{				for(k = 0; k < SIZE; k++)				{					*(*(ptrThree + j) +i) += *(*(ptrOne + k) + i) * *(*(ptrTwo + i) + k);					printf("%5d ", *(*(ptrThree +i) + j));				}				printf("\n");			}		}}int main(){	//initialize the arrays and fill them up with zeros	int arrayOne[SIZE][SIZE] = {};	int arrayTwo[SIZE][SIZE] = {};	int arrayThree[SIZE][SIZE] = {};	//make pointers out of the arrays that we can use	int (*ptrOne)[SIZE] = arrayOne;	int (*ptrTwo)[SIZE] = arrayTwo;	int (*ptrThree)[SIZE] = arrayThree;	//init som variables	int choice;	//start up a while and look for menu choices	while(choice = menu())	{		switch(choice)		{			case 1:			{				enterArray(ptrOne,ptrTwo);				break;			}			case 2:			{				printArray(ptrOne,ptrTwo);				break;			}			case 3:			{				addArray(ptrOne,ptrTwo,ptrThree);				break;			}			case 4:			{				transcendArray(ptrOne,ptrTwo);				break;			}			case 5:			{				multiplyArray(ptrOne,ptrTwo,ptrThree);				break;			}		}	}	}