Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

10-29-2015 , 04:38 PM
Quote:
Originally Posted by Alobar
Its a PIC32MX795F512L

all the documentation is here

http://www.microchip.com/wwwproducts...#documentation

this part deals specifically with the I/O and registers

http://ww1.microchip.com/downloads/e...Doc/61120D.pdf

and this talks about input capture

http://ww1.microchip.com/downloads/e...Doc/61122F.pdf

The button is read through on pin 4 of the board which is here

http://www.digilentinc.com/Data/Prod...T_Max32_RM.pdf (page 11).


I've spent a good number of hours now reading all this stuff and I'm still hugely confused, so I appreciate any help!
Well pin 4 is a digital input and I am not sure what port it is associated with but I would guess port F. The documentation links have the info you need but I won't have the chance to study it for a few days. I don't want to do your assignment for you but PIC is popular enough that I will spend some time on it. I am responding from my iPad as I won't have access to my notebook for a few days and I'll highlight a few things to focus on at that time. It looks like Pin 4 on the device is interfaced to PIC pin 72. If you can determine what port pin 72 is accessed on then there are numerous examples on the Internet in reading I/O pins in PIC assembly language. Once you know that you should be able to write a loop of some sort waiting for the next button press. Btw usually things like button presses are "debounced" because pressing a button isn't initially a "smooth" operation in that the input goes on and off before reaching a steady state.
Programming homework and newbie help thread Quote
10-30-2015 , 10:00 AM
Quote:
Originally Posted by daveT
This looks really interesting. Have you tried out NAND to Tetris yet? I've been meaning to get to that one for years.
That sounds cool. I'm working on an ambitious side project at the moment, but would be neat to try that someday.
Programming homework and newbie help thread Quote
11-08-2015 , 04:29 PM
ayo,

I have this exercise for my C lab in which I have to write a function that takes two arrays and their respective sizes as arguments and returns an array containing their common values. I started writing the code, but got stuck at inserting the common values in the third array.

Eventually I gave up and started testing it only with the main function and the best result I got is printing out either the first common value found or the last as many times as a common value was found...

For example, if array1 is {1, 2, 3, 4, 5} and array2 is {0, 0, 1, 7, 8, 5, 4} the closest I got was printing 1, 1, 1 or 5, 5, 5.

Here is the code:

Spoiler:

The spoiler levels the indentation, sorry about that.

#include <stdio.h>
#include <stdlib.h>

main()

{
int array1[10], array2[10], arraySize1, arraySize2, step1, step2, i, j, k, arraySize3 = 0, *array3;

printf("Enter the size of the first array: \n");
scanf("%d", &arraySize1);

printf("Enter the elements of the first array: \n");

for ( step1 = 0; step1 < arraySize1; step1 ++ )
scanf("%d", &array1[step1]);

printf("Enter the size of the second array: \n");
scanf("%d", &arraySize2);

printf("Enter the elements of the second array: \n");

for ( step2 = 0; step2 < arraySize2; step2 ++ )
scanf("%d", &array2[step2]);

for ( i = 0; i < arraySize1; i++ )
for ( j = 0; j < arraySize2; j++ )
if ( array1[i] == array2[j] )
arraySize3 ++;

array3 = (int*)malloc(arraySize3*sizeof(int));

for ( k = 0; k < arraySize3; k++ )
{
for ( i = 0; i < arraySize1; i++ )
{
for ( j = 0; j < arraySize2; j++ )
{
if ( array1[i] == array2[j] )
break;
array3[k] = array1[i];

}

}
}


printf("\n");

for ( k = 0; k < arraySize3; k ++ )
printf("%d ", array3[k]);

}


And this is the part I'm struggling with:

Spoiler:

for ( k = 0; k < arraySize3; k++ )
{
for ( i = 0; i < arraySize1; i++ )
{
for ( j = 0; j < arraySize2; j++ )
{
if ( array1[i] == array2[j] )
break;
array3[k] = array1[i];
}}}


I want to increment i when a common value is found so that the for starts from i = 1, but it seems that when it breaks, it goes in the k for again and it starts all over again, braking and storing the first/last value, depending on how I change some things in this block. I can't find a solution.

Sorry if my logic is bad or if the code is disgusting, but it feels that I lack some syntax manipulation skills or knowledge of how to enclose statements correctly. I tried mixing it up with some while's, or do while's, but again, the closest I got was to printing out 1,1,1 or 5,5,5.

I know how to transfer the logic into a separate function and call it and all, I just need some clues for this bit, storing the common values in the third array.

I just moved on with my life and solved other exercises, but this keeps nagging me at the back of my brain.

Thanks.
Programming homework and newbie help thread Quote
11-08-2015 , 07:22 PM
Quote:
Originally Posted by woe
ayo,

I have this exercise for my C lab in which I have to write a function that takes two arrays and their respective sizes as arguments and returns an array containing their common values. I started writing the code, but got stuck at inserting the common values in the third array.

Eventually I gave up and started testing it only with the main function and the best result I got is printing out either the first common value found or the last as many times as a common value was found...

For example, if array1 is {1, 2, 3, 4, 5} and array2 is {0, 0, 1, 7, 8, 5, 4} the closest I got was printing 1, 1, 1 or 5, 5, 5.

Here is the code:

Code:
The spoiler levels the indentation, sorry about that.

#include <stdio.h>
#include <stdlib.h>

main()

{
	int array1[10], array2[10], arraySize1, arraySize2, step1, step2, i, j, k, arraySize3 = 0, *array3;

	printf("Enter the size of the first array: \n");
	scanf("%d", &arraySize1);

	printf("Enter the elements of the first array: \n");

	for ( step1 = 0; step1 < arraySize1; step1 ++ )
		scanf("%d", &array1[step1]);

	printf("Enter the size of the second array: \n");
	scanf("%d", &arraySize2);

	printf("Enter the elements of the second array: \n");

    for ( step2 = 0; step2 < arraySize2; step2 ++ )
        scanf("%d", &array2[step2]);

    for ( i = 0; i < arraySize1; i++ )
       for ( j = 0; j < arraySize2; j++ )
        if ( array1[i] == array2[j] )
          arraySize3 ++;

    array3 = (int*)malloc(arraySize3*sizeof(int));

    for ( k = 0; k < arraySize3; k++ )
    {
      for ( i = 0; i < arraySize1; i++ )
      {
        for ( j = 0; j < arraySize2; j++ )
        {
         if ( array1[i] == array2[j] )
            break;
          array3[k] = array1[i];

        }

      }
    }


    printf("\n");

    for ( k = 0; k < arraySize3; k ++ )
      printf("%d ", array3[k]);

}
Embedded System Design on a ShoestringAnd this is the part I'm struggling with:

Code:
for ( k = 0; k < arraySize3; k++ )
    {
      for ( i = 0; i < arraySize1; i++ )
      {
        for ( j = 0; j < arraySize2; j++ )
        {
         if ( array1[i] == array2[j] )
            break;
          array3[k] = array1[i];
     }}}
I want to increment i when a common value is found so that the for starts from i = 1, but it seems that when it breaks, it goes in the k for again and it starts all over again, braking and storing the first/last value, depending on how I change some things in this block. I can't find a solution.

Sorry if my logic is bad or if the code is disgusting, but it feels that I lack some syntax manipulation skills or knowledge of how to enclose statements correctly. I tried mixing it up with some while's, or do while's, but again, the closest I got was to printing out 1,1,1 or 5,5,5.

I know how to transfer the logic into a separate function and call it and all, I just need some clues for this bit, storing the common values in the third array.

I just moved on with my life and solved other exercises, but this keeps nagging me at the back of my brain.

Thanks.
Looks like you should swap the break statement with the array3[k] = array1[i] statement

Last edited by adios; 11-08-2015 at 07:29 PM.
Programming homework and newbie help thread Quote
11-08-2015 , 11:41 PM
woe,

You shouldn't be looping through array3, but instead be tracking which index to assign to with a counter variable, similar to how you calculated the desired size of array3
Programming homework and newbie help thread Quote
11-09-2015 , 12:00 AM
Also, statements like 'break', 'continue', etc. only apply to the block of code they directly reside within. So your 'break' is just exiting your most internal loop and continuing execution in the second loop.
Programming homework and newbie help thread Quote
11-10-2015 , 01:20 PM
are the arrays guaranteed to be sorted? if so I think this would work

Code:
int i = 0;
int j = 0;
int k = 0;
while ((i < size1) && (j < size2)) {
    if (array1[i] < array2[j]) i++;
    else if (array1[i] > array2[j]) j++;
    else {
        array3[k] = array1[i];
        i++;
        j++;
        k++;
    }
}
Programming homework and newbie help thread Quote
11-10-2015 , 02:46 PM
nah, they're not sorted.

after some thinking I came up with this function, which works unless one of the common values occurs more than once. If arr1 = { 1, 2, 3, 4} arr2 = { 1, 2, 2, 7, 8, 4, 4, 4} it gets confused. It works pretty well though compared to what I came up in the weekend. Any suggestions?

Code:
void comValues ( int *arr1, int *arr2, int arrSize1, int arrSize2 ){

	int i, j, k, arr3[10], arrSize3 = 0;

	for ( i = 0; i < arrSize1; i++ )
       for ( j = 0; j < arrSize2; j++ )
          if ( arr1[i] == arr2[j] )
             {
                 if (arrSize3==0)
                      {
                            arr3[arrSize3]= arr1[i];
                                arrSize3++;
                      }
                 else
                 for ( k = 0; k < arrSize3; k++ )
                     if (arr1[i] == arr3[k]) break;
                        else
                        {
                          arr3[arrSize3]= arr1[i];
                          arrSize3++;
                        }
             }

      printf("\n%d\n", arrSize3);

      for ( k = 0; k < arrSize3; k ++ )
      printf("%d ", arr3[k]);
}
Programming homework and newbie help thread Quote
11-10-2015 , 04:51 PM
I don't really understand your code, why are you checking if arrsize3 == 0? and why are you looping through array 3 if its not? (why are you looping through array3 at all?)

why don't you just use the same nested loops that got you what the size of the 3rd array is supposed to be, if in fact that is the correct size? (Was that code given, or did you write it?)

Code:
for ( i = 0; i < arraySize1; i++ ) {
    for ( j = 0; j < arraySize2; j++ ) {
        if ( array1[i] == array2[j] ) {
            array3[k] == array1[i];
            k++;
        }
    }
}
also, I dont really understand if you want dups or not. Is it supposed to be the intersection of 2 arrays or what?

does {1, 5, 7, 9} {1, 1, 2, 3, 4} = {1} or do you want {1, 1} or {1, 1, 1}

Last edited by Alobar; 11-10-2015 at 04:59 PM.
Programming homework and newbie help thread Quote
11-10-2015 , 06:27 PM
I don't want dups. So in your example, I only want {1}.

Also, this:

Code:
for ( i = 0; i < arraySize1; i++ ) {
    for ( j = 0; j < arraySize2; j++ ) {
        if ( array1[i] == array2[j] ) {
            array3[k] == array1[i];
            k++;
        }
    }
won't work, because if we initialize k = 0; and if it finds a common value at i = 1, it will put the element in array3[1], leaving array3[0] empty. The same is true for any leaps in i greater than one. So, unless the common values are consecutive, the program, when compiled and run, will return some bogus values like 4345264246, because it accesses memory with nothing in it. I've tried it.

I'll try to explain my function:
Code:
for ( i = 0; i < arrSize1; i++ )
       for ( j = 0; j < arrSize2; j++ )
          if ( arr1[i] == arr2[j] )
             {   
                 if (arrSize3==0) //this initializes the intersection array (arr3), ie the first element
                      {
                            arr3[arrSize3]= arr1[i];
                                arrSize3++;
                      }
                 else 
                 for ( k = 0; k < arrSize3; k++ ) //iterate through arr3 to check for dups
                     if (arr1[i] == arr3[k]) break; //if we found a dup, move on
                        else
                        {
                          arr3[arrSize3]= arr1[i]; //if we didn't, add the new element to arr3
                          arrSize3++;
                        }
             }
I wrote the code from scratch.

Last edited by woe; 11-10-2015 at 06:28 PM. Reason: I hate C
Programming homework and newbie help thread Quote
11-10-2015 , 07:08 PM
Quote:
Originally Posted by woe
I don't want dups. So in your example, I only want {1}.

Also, this:

Code:
for ( i = 0; i < arraySize1; i++ ) {
    for ( j = 0; j < arraySize2; j++ ) {
        if ( array1[i] == array2[j] ) {
            array3[k] == array1[i];
            k++;
        }
    }
won't work, because if we initialize k = 0; and if it finds a common value at i = 1, it will put the element in array3[1], leaving array3[0] empty.

no it wont, k is only incremented if a match has been found. So regardless of what your i and j values are, unless there has been a match, k stays at zero. After you find a match, K is moved to 1, then it stays at 1 until another math is found, doesnt matter what i and j are. Go over the execution by hand of nested loops until you get the hang of it.

the above program wouldnt work tho because it will get all the dups in array 2 of the item its looking at in array 1 (since it checks all of array2 for each item in array1), so you would need to add a break statement.


The language here is kind of irrelevant to the problem. You can hate C but doing this in another language wouldn't be much different, you still think about the problem the same, you are just using different syntax to explain the same looping logic. You just need to get used to thinking about how loops and nested loops function, which dont get frustrated with cuz it's not like we normally sit around and think about **** like that, so its hard at first.

Last edited by Alobar; 11-10-2015 at 07:15 PM.
Programming homework and newbie help thread Quote
11-10-2015 , 07:22 PM
Write a contains function?
Programming homework and newbie help thread Quote
11-10-2015 , 08:18 PM
I had an easier time getting this to work in ruby.
Programming homework and newbie help thread Quote
11-10-2015 , 08:20 PM
@alobar - I completely spaced the PIC code issue you were having. Still an issue?
Programming homework and newbie help thread Quote
11-10-2015 , 08:21 PM
Quote:
Originally Posted by woe
I had an easier time getting this to work in ruby.
Translate your ruby algorithm to C, seriously.
Programming homework and newbie help thread Quote
11-10-2015 , 08:33 PM
Quote:
Originally Posted by adios
@alobar - I completely spaced the PIC code issue you were having. Still an issue?
No, I figured it out, thanks tho!!!!
Programming homework and newbie help thread Quote
11-10-2015 , 10:39 PM
C# version:

Code:
public static int[] IntersectingArray(this int[] array1, int[] array2) {
    if(array1 == null || array2 == null) return null;
    return array1.Intersect(array2).ToArray();
}
Programming homework and newbie help thread Quote
11-10-2015 , 10:44 PM
quit showing off!
Programming homework and newbie help thread Quote
11-11-2015 , 02:03 AM
woe;

Start by thinking about the simplest case first. Suppose Array2 has zero elements, then think about if Array2 had one element, etc.
Programming homework and newbie help thread Quote
11-11-2015 , 11:18 AM
Quote:
Originally Posted by Roonil Wazlib
Write a contains function?
Woe,

This is good advice and I think an important engineering principle, abstraction, can be revealed here. Your algorith does several separate things, but they are all crammed together. If you pull out some of the logic, it starts to get really simple.

If you wrote a 'contains' function that takes an array and a value and just returns whether or not that array contains that value, your entire algorithm can be simplified like this:

Code:
for ( i = 0; i < arrSize1; i++ )
{
    if ( contains(arr2, arr1[i]) && !contains(arr3, arr1[i]) )
    {
        arr3[arrSize3] = arr1[i];
        arrSize3++;
    }
}
The result ends up being so much more readable. Taking it a step further with more descriptive variable names:

Code:
for ( i = 0; i < array1size; i++ )
{
    int element = array1[i];
    if ( contains(array2, element) && !contains(commonElements, element) )
    {
        commonElements[commonElementsSize] = element;
        commonElementsSize++;
    }
}
You can obviously take in even further by abstracting things like adding it to the common elements array (unique values only), etc. The more human-readable your code gets, the better off you are.

Last edited by Benholio; 11-11-2015 at 11:34 AM.
Programming homework and newbie help thread Quote
11-11-2015 , 01:08 PM
obv i dunno how far along into learning he is, but i've had teachers in entry-level classes that get mad if you try to do anything too advanced, like not putting everything into the main function

maybe he's past that, maybe he isn't. probably a good question for his prof
Programming homework and newbie help thread Quote
11-15-2015 , 06:48 PM
What is the best practice for handling errors in C?

I've been writing an expression interpreter that works by recursive descent, and errors could arise in one of several nested functions. Each error will call a function that will set the global int errno to some nonzero number, and the main function will print an error message if there is one.

But I also want to stop evaluating the expression after the first error occurs. Does that mean that I have to put in error checks in all of the helper functions (sometimes more than one per function)? Is there a better way to do this?

For example, one of the functions looks like this:

Code:
double BoolExp(void)
{
	double Value;
	
	if (!errno)
		Value = Equivalence();
	while (Look == '&' || Look == '|' && !errno) {
		if (Look == '&') {
			Match('&');
			Value = (Equivalence() && Value);
		} else {
			Match('|');
			Value = (Equivalence() || Value);
		}
	}
	return Value;
}
Here an error could arise each time Equivalence() is called. It's also possible that error could arise when BoolExp is called since it is defined recursively (indirectly). (Although I guess I could add a check to prevent BoolExp from being called again if there has already been an error.)
Programming homework and newbie help thread Quote
11-15-2015 , 07:14 PM
You can have the return value of the function be a result code and use a reference variable for the output value.

Code:
int Equivalence(double *Value) {
  if ( /* some error case */ )
    return -1;

  /* equivalence logic */

  *Value = stuff_from_logic;
  return 0;
}
Then after you call Equivalence anywhere you check the return code and if it is non-zero you know its an error and can handle it. Different negative values can represent different error cases. Probably best to define constants or enums for each one instead of using magic numbers.

http://stackoverflow.com/questions/3...ling-in-c-code

Last edited by Wolfram; 11-15-2015 at 07:28 PM.
Programming homework and newbie help thread Quote
11-16-2015 , 08:48 PM
Quote:
Originally Posted by econophile
What is the best practice for handling errors in C?

I've been writing an expression interpreter that works by recursive descent, and errors could arise in one of several nested functions. Each error will call a function that will set the global int errno to some nonzero number, and the main function will print an error message if there is one.

But I also want to stop evaluating the expression after the first error occurs. Does that mean that I have to put in error checks in all of the helper functions (sometimes more than one per function)? Is there a better way to do this?

For example, one of the functions looks like this:

Code:
double BoolExp(void)
{
	double Value;
	
       try {
	
		Value = Equivalence();
                 
                while (( '&' == Look) || ('|' == Look)) {
		    if ('&' == Look) {
			Match('&');
			Value = (Equivalence() && Value);
		    } else {
			Match('|');
			Value = (Equivalence() || Value);
                     }

		 }

                 return Value;
         }
         catch {

              // handle exception
	}
	
}
Here an error could arise each time Equivalence() is called. It's also possible that error could arise when BoolExp is called since it is defined recursively (indirectly). (Although I guess I could add a check to prevent BoolExp from being called again if there has already been an error.)
I would recommend using C++ exception handling since C is a subset of C++. Google is your friend regarding exception handling. You could have your functions throw exceptions when errors are detected.
Programming homework and newbie help thread Quote
11-17-2015 , 05:27 AM
Quote:
Originally Posted by adios
I would recommend using C++ exception handling since C is a subset of C++. Google is your friend regarding exception handling. You could have your functions throw exceptions when errors are detected.
but then he's no longer using C, he's using C++.
Programming homework and newbie help thread Quote

      
m