#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Need help with homework assignment


    Hello guys,
    I am learning right now pointers and I need some help with the homework assignment.

    The program needs to display a menu with 5 choices:
    1. Will check the existence of an arithmetic progression on the diagonal line of the matrix, and prints if there is one on the main diagonal, on the second one or not at all.
    2. Will print a reverse array from the center
    3. will calculate the average of the matrix Columns
    4. will make a linear search of all the times a number is exist in a one-dimensional array, print it and the address of the places its appears.
    5. Will Exit the program

    I have managed to write a program,
    However, I still have some bugs:
    1. The program should display the menu until the user type 5, but it does not.
    2. In choice 1, no matter what, it keep printing me that there are “No diagonal line in the matrix”
    3. In choice 2, when the program printing the reverse array, its print the left number of the array near by the line “the new array is”
    4. In choice 4, its keep crashing the program.

    Appreciate any help,
    and sorry for the novice mistakes. ;)

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    int num,num2,size,size2,size3;
    
    int input_num (void);
    int existance_of_arithmetic_progression(int mat[7][7], int *diaganol2);
    void reverse_array(int *array1);
    void AverageColumns(int mat2[][7],float col_avg[], int r, int c);
    float Average(int temp[],int r);
    int Search(int num2,int *arr,int *search_ptr[],int size3);
    void main()
    {   
    	int i,j,diaganol1,diaganol2=1,r,c,count;
    	int mat[7][7],mat2[5][7];
    	int *array1,*arr,*search_ptr[7];
    	float col_avg[7],*p;
    	num=input_num();
    		switch (num)
    		{
    			case 1:{ printf("enter the matrix size:\n");
    					 scanf("%d",&size);
    					 printf("enter the matrix numbers:\n");
    					for (i=0;i<size;i++)
    						for (j=0;j<size;j++)
    						{printf("the number in place [%d][%d] will be:",i,j);
    						scanf("%d",&mat[i][j]);}
    					 diaganol1=existance_of_arithmetic_progression (mat,&diaganol2);
    					 if(diaganol1==1)
    						 printf("there is an arithmetic progression on the main diagonal of the matrix\n");
    					 if(diaganol2==1)
    						 printf("there is an arithmetic progression on the second diagonal of the matrix\n");
    					 if((diaganol1==0)&&(diaganol2==0));
    						printf("No diagonal line in the matrix\n");
    						break;
    				   }
    			case 2: { printf("enter size of the array (minimum 1 and maximum 10):\n");
    					  scanf("%d",&size2);
    					  while ((size2<1)||(size2>10))
    						  { printf("wrong input! enter correct size of the array:\n");
    							scanf("%d",&size2);}
    					  printf("enter the array numbers:\n");
    					  array1=(int*) malloc(size2*sizeof(int));
    					  for (i=0;i<size2;i++)
    						  scanf("%d",(array1+i));
    					  printf("%d",*array1);
    					  reverse_array(array1);
    					  break;
    					}
    			case 3: { printf("enter the matrix size ---> Rows (max 5) then press enter, Columns (max 7):\n");
    					  scanf("%d%d",&r,&c);
    					  while ((c>7)||(r>5))
    						{printf("wrong input! enter again: Rows (max 5) then press enter, Columns (max 7):\n");
    						 scanf("%d%d",&r,&c);}
    					   printf("enter the matrix numbers:\n");
    					  for (i=0;i<r;i++)
    							for (j=0;j<c;j++)
    								{printf("the number of matrix[%d][%d] is ",i,j);
    								scanf("%d",&mat2[i][j]);}
    						
    					    AverageColumns(mat2,col_avg,r,c);
    					
    						break;
    					}
    			case 4: {
    						printf("enter size of the array (maximum 7):\n");
    					  scanf("%d",&size3);
    					  while ((size3<1)||(size3>7))
    						  { printf("enter size of the array:\n");
    							scanf("%d",&size3);}
    					  printf("enter the array numbers:\n");
    					  arr= (int*) malloc(size3*sizeof(int));
    					  for (i=0;i<size3;i++)
    						  scanf("%d",(arr+i));
    					  printf("enter the number you are looking for:\n");
    						  scanf("%d",&num2);
    					 count=  Search (num2,arr,search_ptr,size3);
    					 switch (count)
    					 {
    						case 0: printf("there is no numbers in the array\n");break;
    						case 1: printf("%d appers %d times in the array in places %d at the address %x\n",num2,count,*(search_ptr[0]),search_ptr);break;
    						case 2: printf("%d appers %d times in the array in places %d %d at the address %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),search_ptr[0],search_ptr[1]);break;
    						case 3: printf("%d appers %d times in the array in places %d %d %d at the address %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),search_ptr[0],search_ptr[1],search_ptr[2]);break;
    						case 4: printf("%d appers %d times in the array in places %d %d %d %d at the address %x %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),*(search_ptr[3]),search_ptr[0],search_ptr[1],search_ptr[2],search_ptr[3]);break;
    						case 5: printf("%d appers %d times in the array in places %d %d %d %d %d at the address %x %x %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),*(search_ptr[3]),*(search_ptr[4]),search_ptr[0],search_ptr[1],search_ptr[2],search_ptr[3],search_ptr[4]);break;
    						case 6: printf("%d appers %d times in the array in places %d %d %d %d %d %d at the address %x %x %x %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),*(search_ptr[3]),*(search_ptr[4]),*(search_ptr[5]),search_ptr[0],search_ptr[1],search_ptr[2],search_ptr[3],search_ptr[4],search_ptr[5]);break;
    						case 7: printf("%d appers %d times in the array in places %d %d %d %d %d %d %d at the address %x %x %x %x %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),*(search_ptr[3]),*(search_ptr[4]),*(search_ptr[5]),*(search_ptr[6]),search_ptr[0],search_ptr[1],search_ptr[2],search_ptr[3],search_ptr[4],search_ptr[5],search_ptr[6]);break;
    					 }
    					}
    
    			case 5: break;
    		}
    	
    	
    	system("pause");
    }
    //---------------------------------------------------------------------------------------------------------------
    int input_num(void)
    {
    		
    		do
    	{
    		printf("Hello, Please choose a number\n\n");
    	    printf("1. will check the existence of an arithmetic progression\n   on the diagonal line of the matrix\n");
    	    printf("2. will build a reverse array from the center\n");
    		printf("3. will calculate the average of the matrix Columns\n");
    		printf("4. will make a linear search of all the times a number is\n   exist in a one-dimensional array\n");
    		printf("5. will Exit the program\n");
    		scanf("%d",&num);
    	}
    
    		while (!(num>=1)&&(num<5));
    	return num;
    }
    
    int existance_of_arithmetic_progression(int mat[7][7],int *diaganol2)
    {    
    	int i,i2=size;
    		for (i=1; i<= size-2; i++)
    	{   
    		if ((mat[i][i2-1]-mat[i-1][i2])!=(mat[i+1][i2-2]-mat[i][i2-1]))
    		{
    			*diaganol2=0;
    			i2--;
    		}
    		if ((mat[i][i]-mat[i-1][i-1])!=(mat[i+1][i+1]-mat[i][i]))
    			return 0;
    	}
    	return 1;
    }
    void reverse_array(int *array1)
    {
    	int i,*array2;
    	array2= (int*) malloc (size2*sizeof(int));
    	for (i=0;i<size2;i++)
    		*(array2+i)= *(array1+size2-1-i);
    	printf("the new array is:\n");
    	for (i=0;i<size2;i++)
    			printf("%d",*(array2-size+i));
    }
    
    void AverageColumns(int mat2[][7],float col_avg[], int r, int c)
    {
    	int i,j,temp[5];
    	for (j=0;j<c;j++)
    		{
    			for (i=0;i<r;i++)
    				temp[i]= mat2[i][j];
    		col_avg[j]= Average(temp,r);
    		}
    	for (j=0;j<c;j++)
    		printf("%.2f",col_avg[j]);
    	
    	
    }
    
    float Average(int temp[],int r)
    {
     int i;
     float sum=0;
     for (i=0;i<r;i++)
    	sum = sum+temp[i];
     sum= sum/(float)r;
     return (float)sum;
    }
    
    int Search(int num2,int *arr,int *search_ptr[],int size3)
    {
    	int i,count=0;
    	for (i=0;i<size3;i++)
    		if (*(arr+i)==num2)
    			//CR not right use in the array
    			{ *search_ptr = (arr+i);
    				count++;}
    	return count;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Why are you ignoring warnings? Your program is seriously broken as the warnings plainly indicate:
    C:TEST>gcc -Wall haim01.c
    haim01.c:13: warning: return type of `main' is not `int'
    haim01.c: In function `main':
    haim01.c:81: warning: unsigned int format, pointer arg (arg 5)
    haim01.c:82: warning: unsigned int format, pointer arg (arg 6)
    haim01.c:82: warning: unsigned int format, pointer arg (arg 7)
    haim01.c:83: warning: unsigned int format, pointer arg (arg 7)
    haim01.c:83: warning: unsigned int format, pointer arg (arg 8)
    haim01.c:83: warning: unsigned int format, pointer arg (arg 9)
    haim01.c:84: warning: unsigned int format, pointer arg (arg 8)
    haim01.c:84: warning: unsigned int format, pointer arg (arg 9)
    haim01.c:84: warning: unsigned int format, pointer arg (arg 10)
    haim01.c:84: warning: unsigned int format, pointer arg (arg 11)
    haim01.c:85: warning: unsigned int format, pointer arg (arg 9)
    haim01.c:85: warning: unsigned int format, pointer arg (arg 10)
    haim01.c:85: warning: unsigned int format, pointer arg (arg 11)
    haim01.c:85: warning: unsigned int format, pointer arg (arg 12)
    haim01.c:85: warning: unsigned int format, pointer arg (arg 13)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 10)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 11)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 12)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 13)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 14)
    haim01.c:86: warning: unsigned int format, pointer arg (arg 15)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 11)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 12)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 13)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 14)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 15)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 16)
    haim01.c:87: warning: unsigned int format, pointer arg (arg 17)
    haim01.c:17: warning: unused variable `p'

    C:TEST>
    Lines 81 though 87 are:
    Code:
                            case 1: printf("%d appers %d times in the array in places %d at the address %x\n",num2,count,*(search_ptr[0]),search_ptr);break;
                            case 2: printf("%d appers %d times in the array in places %d %d at the address %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),search_ptr[0],search_ptr[1]);break;
                            case 3: printf("%d appers %d times in the array in places %d %d %d at the address %x %x %x\n",num2,count,*(search_ptr[0]),*(search_ptr[1]),*(search_ptr[2]),search_ptr[0],search_ptr[1],search_ptr[2]);break;
    You're telling printf to display a pointer as if it were a number. If that case weren't crashing you'd be complaining about it printing garbage.

    Problem #1: You appear to be expecting main to be running a loop. However, there is no loop in main. If you want main to run a loop, then you need to write in a loop.

    Problem #2:
    Code:
         if((diaganol1==0)&&(diaganol2==0));
            printf("No diagonal line in the matrix\n");
    If both diaganol1 and diagonal2 are zero, then you do nothing (which is what you are telling the computer to do). Then you print "No diagonal line in the matrix\n" regardless of the values of those two variables.

    Maybe you want to lose that extra semicolon.

    Problem #3: Step through that case either with a debugger or by hand. If by hand, then be very meticulous about doing exactly what the code tells you to do, not what you "know" it says.

    Problem #4: Lunch hour is over. Gotta get back to work. Step through that case with a debugger to see where it crashes.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    case 2, line 43:
    Code:
                          array1=(int*) malloc(size2*sizeof(int));
                          for (i=0;i<size2;i++)
                              scanf("%d",(array1+i));
                          printf("%d",*array1);
                          reverse_array(array1);
                          break;
    Refer to the line in red. What are you trying to do there? All it does do is to print the first element of array1 (AKA "the left number of the array near by the line “the new array is” ").

    If that is what you wanted it to do, then why do you have it listed as Problem #3?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    thanks guys,
    I will try it out

IMN logo majestic logo threadwatch logo seochat tools logo