What's new
Fantasy Football - Footballguys Forums

Welcome to Our Forums. Once you've registered and logged in, you're primed to talk football, among other topics, with the sharpest and most experienced fantasy players on the internet.

Anybody good at C programming? (1 Viewer)

ADP

Footballguy
I have a problem I have been racking my brain with it. I am sure this would take all of 5 mins for a knowledgeable programmer; however, that I am not.

Question:

Create two arrays of integers, each holding ten elements of data. Create a third array of integers for a result array. Your main program will take the two arrays of integers and pass them to the function subtract(). Inside the function, subtract the first array from the second array and put the difference into the third array. Print the values of all three arrays back in the main program.

Any help would be much appreciated.

 
Luckily not a computer degree. Only computer class I have, and thus far, only problem i have not been able to figure out. I understand arrays mostly. It is the function part of it where I have to have it subtract the two arrays and place in array 3 that is getting me.

 
Luckily not a computer degree. Only computer class I have, and thus far, only problem i have not been able to figure out. I understand arrays mostly. It is the function part of it where I have to have it subtract the two arrays and place in array 3 that is getting me.
are you just trying to subtract the value in array1 from the value in the corresponding position in array2?

 
From what I understand, that's just a hybrid of C and C++. If you can give me idea, I am sure I can start to comprehend enough to do it in C.

 
function int[] Subtract(int[] array1, int[] array2)

{

var array3 = new int[10];

for (var i=0; i<array2.Length; i++)

{

array3 = array2 - array1;

}

return array3;

}

In Main, you can just loop through each array and print all the values.

 
I never took C, but how about some M

Code:
EN	;	F X=1:1:10 R !,"Enter a value for Array 1: ",Z S A1(X)=Z R !,"Enter a value for Array 2: ",Z S A2(X)=Z	D SUB	W ! F X=1:1:10 D	.W !,A2(X),?10,"- ",A1(X),?25,"= ",A3(X)	K X,Z,A1,A2,A3	Q	;SUB	F X=1:1:10 S A3(X)=A2(X)-A1(X)	Q
 
Think i got it....thanks for help.

#include <stdio.h>
#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()
{
int arr1, arr2, arr3;
int i;

printf ("Please enter 10 numbers for the first array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");
for (i=0; i<SIZE; i++)
printf("%d ", arr3);

return 0;
}
void subtract(int arr1[], int arr2[], int arr3[])
{
int i;

for (i=0; i<SIZE; i++)
arr3 = arr2 - arr1;
}
 
You don't understand the question so providing the answer won't help you. Consider a different major/career.
What? I provided that answer. I just needed some help in understanding it. And this is not a major/career, it was just one class I took as an option. I already have one degree in meteorology.

 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.

 
Last edited by a moderator:
What? I provided that answer. I just needed some help in understanding it. And this is not a major/career, it was just one class I took as an option. I already have one degree in meteorology.

You don't understand the question so providing the answer won't help you. Consider a different major/career.
Settle down bro.

Congrats on the meteorology degree.

 
Last edited by a moderator:
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.
Just when i though I was out of the woods....you pull me back in...#$#@. I'll see if I can make those changes.

 
Ffs, staring at netbeans right now reeling because I can't remember how to code...

Took a year off school a semester before graduating because of health reasons and I feel like I don't remember anything.

 
Think i got it....thanks for help.

#include <stdio.h>
#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()
{
int arr1, arr2, arr3;
int i;

printf ("Please enter 10 numbers for the first array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");
for (i=0; i<SIZE; i++)
printf("%d ", arr3);

return 0;
}
void subtract(int arr1[], int arr2[], int arr3[])
{
int i;

for (i=0; i<SIZE; i++)
arr3 = arr2 - arr1;
}
Consider detecting the size of both arrays instead of relying on a hardcoded constant.

 
Royally confused by that. lol
In M you can create variables on the fly without declaring them.

Starting at the top:

For loop incrementing X from 1 by 1 up to 10. On each pass prompt to the screen: line feed, "Enter a value..." and take the answer as Z, set the array A1(whatever X is) to the value Z. Do another line feed, prompt the user for the value for A2 array, their answer is Z, set A2(x) to that. This will repeat 10 times.

Do subroutine SUB

In SUB, another For Loop and you simply set A3(X) to the results of A2(x)-A1(X)

then quit and return to where SUB was called.

Do a line feed. Then another For loop recycling the value X again 1 by 1 to 10. The next line with the "." could be done on the same line, but just for kicks I put it in an on the fly subroutine with the D(O) command.

That line writes a line feed, then the value of A2(x) then tab to the 10th column and "-" then the value of A1(X) tab to the 25th column and "=" then the value of A3(x)

Kill the variables used

Quit

Clear as mud

 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.
can you show me in my code where you would put the arr3=subtract(arr1, arr2). I understand swapping out the arr3 in the function declaration with results at top and bottom of code, as well as changing out the arr3 in the actuall function itself with results. Just not too sure about the main section. Currently have subtract(arr1, arr2, arr3); Are you saying replace that?

 
Think i got it....thanks for help.

#include <stdio.h>
#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()
{
int arr1, arr2, arr3;
int i;

printf ("Please enter 10 numbers for the first array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");
for (i=0; i<SIZE; i++)
printf("%d ", arr3);

return 0;
}
void subtract(int arr1[], int arr2[], int arr3[])
{
int i;

for (i=0; i<SIZE; i++)
arr3 = arr2 - arr1;
}
Consider detecting the size of both arrays instead of relying on a hardcoded constant.
Yeh, I am not sure really how to do that, but figured since question said 10 int's specifically I would be ok with the define.

 
Think i got it....thanks for help.

#include <stdio.h>
#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()
{
int arr1, arr2, arr3;
int i;

printf ("Please enter 10 numbers for the first array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");
for (i=0; i<SIZE; i++)
scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");
for (i=0; i<SIZE; i++)
printf("%d ", arr3);

return 0;
}
void subtract(int arr1[], int arr2[], int arr3[])
{
int i;

for (i=0; i<SIZE; i++)
arr3 = arr2 - arr1;
}
Consider detecting the size of both arrays instead of relying on a hardcoded constant.
Yeh, I am not sure really how to do that, but figured since question said 10 int's specifically I would be ok with the define.
That'll be good enough for the class. If that's all you want then you're done

 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.

can you show me in my code where you would put the arr3=subtract(arr1, arr2). I understand swapping out the arr3 in the function declaration with results at top and bottom of code, as well as changing out the arr3 in the actuall function itself with results. Just not too sure about the main section. Currently have subtract(arr1, arr2, arr3); Are you saying replace that?
Actually, you are best off just doing the printing from the subtract function. It's too difficult to explain how to return an array because you would really be returning a pointer, which you probably haven't learned yet.So I would pass in arr1 and arr2, then do the subtract and print in your subtract function.

 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.

can you show me in my code where you would put the arr3=subtract(arr1, arr2). I understand swapping out the arr3 in the function declaration with results at top and bottom of code, as well as changing out the arr3 in the actuall function itself with results. Just not too sure about the main section. Currently have subtract(arr1, arr2, arr3); Are you saying replace that?
Actually, you are best off just doing the printing from the subtract function. It's too difficult to explain how to return an array because you would really be returning a pointer, which you probably haven't learned yet.So I would pass in arr1 and arr2, then do the subtract and print in your subtract function.
Returning a pointer may have been part of the question. Why else would the problem require the results to be printed in the main program instead of the called function?

 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.

can you show me in my code where you would put the arr3=subtract(arr1, arr2). I understand swapping out the arr3 in the function declaration with results at top and bottom of code, as well as changing out the arr3 in the actuall function itself with results. Just not too sure about the main section. Currently have subtract(arr1, arr2, arr3); Are you saying replace that?
Actually, you are best off just doing the printing from the subtract function. It's too difficult to explain how to return an array because you would really be returning a pointer, which you probably haven't learned yet.So I would pass in arr1 and arr2, then do the subtract and print in your subtract function.

Returning a pointer may have been part of the question. Why else would the problem require the results to be printed in the main program instead of the called function?
Oh. Good point. I didn't even read the whole problem statement, just read the code.OK. So with all that said, the easiest thing to do is what you posted earlier. Go ahead and pass all three arrays in and modify arr3 in subtract. It's not the best practice but it works and should be acceptable.

 
Ashamed to say this took me an hour to remember how to do and it still looks like crap.

Java:

Code:
public static void main(String[] args)     {        Scanner input = new Scanner(System.in);        int[] array1 = new int[10];        int[] array2 = new int[10];                        for (int i=0; i<array1.length; i++)        {            System.out.println("Enter the numbers for each element of the first array");            array1[i] = input.nextInt();                                     }                for (int i=0; i<array2.length; i++)        {            System.out.println("Enter the numbers for each element of the second array");            array2[i] = input.nextInt();                                     }                        int[] array3 = subtract(array1, array2);                        System.out.println("Array 1: \n-------------\n");        for (int number : array1)        {           System.out.println("Number = " + number);         }                          System.out.println("\nArray 2: \n-------------\n");        for (int number : array2)        {           System.out.println("Number = " + number);         }                System.out.println("\nArray 3: \n-------------\n");        for (int number : array3)        {           System.out.println("Number = " + number);         }            }        public static int[] subtract(int arrayOne[], int arrayTwo[])    {        int[] arrayThree = new int[10];                for (int i=0; i < arrayTwo.length; i++)        {            arrayThree[i] = arrayTwo[i] - arrayOne[i];        }        return arrayThree;    }
 
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.

can you show me in my code where you would put the arr3=subtract(arr1, arr2). I understand swapping out the arr3 in the function declaration with results at top and bottom of code, as well as changing out the arr3 in the actuall function itself with results. Just not too sure about the main section. Currently have subtract(arr1, arr2, arr3); Are you saying replace that?
Actually, you are best off just doing the printing from the subtract function. It's too difficult to explain how to return an array because you would really be returning a pointer, which you probably haven't learned yet.So I would pass in arr1 and arr2, then do the subtract and print in your subtract function.

Returning a pointer may have been part of the question. Why else would the problem require the results to be printed in the main program instead of the called function?
Oh. Good point. I didn't even read the whole problem statement, just read the code.OK. So with all that said, the easiest thing to do is what you posted earlier. Go ahead and pass all three arrays in and modify arr3 in subtract. It's not the best practice but it works and should be acceptable.
Thanks guys..appreciate the feedback.

 
Ashamed to say this took me an hour to remember how to do and it still looks like crap.

Java:

public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] array1 = new int[10]; int[] array2 = new int[10]; for (int i=0; i<array1.length; i++) { System.out.println("Enter the numbers for each element of the first array"); array1 = input.nextInt(); } for (int i=0; i<array2.length; i++) { System.out.println("Enter the numbers for each element of the second array"); array2 = input.nextInt(); } int[] array3 = subtract(array1, array2); System.out.println("Array 1: \n-------------\n"); for (int number : array1) { System.out.println("Number = " + number); } System.out.println("\nArray 2: \n-------------\n"); for (int number : array2) { System.out.println("Number = " + number); } System.out.println("\nArray 3: \n-------------\n"); for (int number : array3) { System.out.println("Number = " + number); } } public static int[] subtract(int arrayOne[], int arrayTwo[]) { int[] arrayThree = new int[10]; for (int i=0; i < arrayTwo.length; i++) { arrayThree = arrayTwo - arrayOne; } return arrayThree; }

Over my head.....way over. lol

 
kutta said:
ADP said:
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.
standard c allows output and input argumentsshould use a pointer though

 
kutta said:
ADP said:
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.
standard c allows output and input argumentsshould use a pointer though
I'm mostly a c# programmer, and I'm probably mistaken, but I'm pretty sure that your code will not work as intended because array 3 is passed by value (the values altered in subtract() in the array will not persist in main()). hence the need to use a pointer.

if this is a basic programmer class, are you sure it isn't c++?

 
kutta said:
ADP said:
Think i got it....thanks for help.

#include <stdio.h>

#define SIZE 10

void subtract (int arr1[], int arr2[], int arr3[]);

int main()

{

int arr1, arr2, arr3;

int i;

printf ("Please enter 10 numbers for the first array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr1);

printf ("\nPlease enter 10 numbers for the second array: \n");

for (i=0; i<SIZE; i++)

scanf("%d", &arr2);

subtract(arr1, arr2, arr3);

printf ("\n\nResultant third array is: ");

for (i=0; i<SIZE; i++)

printf("%d ", arr3);

return 0;

}

void subtract(int arr1[], int arr2[], int arr3[])

{

int i;

for (i=0; i<SIZE; i++)

arr3 = arr2 - arr1;

}
I wouldn't in pass arr3 into the subtract function. Declare a "results" array in subtract() and use that instead. Then make your subtract function return an array and return results, and in main, set arr3=subtract(arr1, arr2).ETA: it's bad form to pass something to a function and allow that function to manipulate it without really knowing how it is being manipulated. You are manipulating arr3 in subtract but you declared it in main, and you are trying to use it again in main. And actually, you are really just kind of getting lucky if that is working.
standard c allows output and input argumentsshould use a pointer though

I'm mostly a c# programmer, and I'm probably mistaken, but I'm pretty sure that your code will not work as intended because array 3 is passed by value (the values altered in subtract() in the array will not persist in main()). hence the need to use a pointer.

if this is a basic programmer class, are you sure it isn't c++?
its not my code :)
 
I have a problem I have been racking my brain with it. I am sure this would take all of 5 mins for a knowledgeable programmer; however, that I am not.

Question:

Create two arrays of integers, each holding ten elements of data. Create a third array of integers for a result array. Your main program will take the two arrays of integers and pass them to the function subtract(). Inside the function, subtract the first array from the second array and put the difference into the third array. Print the values of all three arrays back in the main program.

Any help would be much appreciated.
I haven't written anything in C in a loooong time... & I don't have a C compiler installed anywhere but i'd do something like this ... well it's almost C... its C++ :

You can't return an int array directly in C,  but you can return an int* which is pretty much the same thing under the hood.

    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int b[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

    int *c = subtract(a, b);
    for (int i = 0; i < 10; ++i)
        printf("a: %d - b:%d = c:%d \n", a, b, c);


 

Since you said pass the two arrays to the subtract function, I'm going to assume where subtract is something like this:

Code:
int* subtract( int*  x,  int*  y)
{
    int* retval = new int[10];// use malloc here for straight c
    for (int i = 0; i < 10; ++i)
        retval = x - y;
    return retval;
}
 
Last edited by a moderator:
I haven't written anything in C in a loooong time... & I don't have a C compiler installed anywhere but i'd do something like this ... well it's almost C... its C++ :

You can't return an int array directly in C,  but you can return an int* which is pretty much the same thing under the hood.

    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int b[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

    int *c = subtract(a, b);
    for (int i = 0; i < 10; ++i)
        printf("a: %d - b:%d = c:%d \n", a, b, c);

 

Since you said pass the two arrays to the subtract function, I'm going to assume where subtract is something like this:

int* subtract( int*  x,  int*  y)
{
    int* retval = new int[10];// use malloc here for straight c
    for (int i = 0; i < 10; ++i)
        retval = x - y;
    return retval;
}
I'm sure he still struggling with this over 2 years later

 
I'm sure he still struggling with this over 2 years later
LOL. I spent the weekend replacing fence posts instead of watching hoops & drinking beer. Im sore, tired & apparently unable to read the timestamp on the OP.

They shoot horses, don't they? :sadbanana:  

 
BowieMercs said:
LOL. I spent the weekend replacing fence posts instead of watching hoops & drinking beer. Im sore, tired & apparently unable to read the timestamp on the OP.

They shoot horses, don't they? :sadbanana:  
Depends...what's your 1/4 mile time Bowie?

You could be a stable pony...you have a calm and reassuring demeanor...that's a big plus!

Also, you could go the stud route...how's your pedigree?  Are ya fixed?

Either way, the Churchill Downs stables are just across the street from me...we have room for ya buddy.  :thumbup:

 
Depends...what's your 1/4 mile time Bowie?

You could be a stable pony...you have a calm and reassuring demeanor...that's a big plus!

Also, you could go the stud route...how's your pedigree?  Are ya fixed?

Either way, the Churchill Downs stables are just across the street from me...we have room for ya buddy.  :thumbup:
Always good to have crash pads in strategic locations :D  

 

Users who are viewing this thread

Top