Is it better to use
#define at the top or should I just use
The problem when I go this route is that I get "Array not initialized or to long" something along those lines.Code:int myarray[10]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Is it better to use
#define at the top or should I just use
The problem when I go this route is that I get "Array not initialized or to long" something along those lines.Code:int myarray[10]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Definitely better to #define.
Code:// To increase the array size, I'd have to make three separate changes. // Imagine what it's like when that value is used in several different // functions, and/or throughout several different source files. #include <stdio.h> int main(void) { int array[10]; int i; for(i=0; i<10; i++) array[i] = i+1; for(i=0; i<10; i++) printf("%d\n",array[i]); return 0; }If you could post a snippet of code, along with the exact warning/error messages you're seeing, perhaps we can figure out the problem you're running into.Code:// To increase the array size, I just change one value - the #define #include <stdio.h> #define MAX_LEN 10 int main(void) { int array[MAX_LEN]; int i; for(i=0; i<MAX_LEN; i++) array[i] = i+1; for(i=0; i<MAX_LEN; i++) printf("%d\n",array[i]); return 0; }
Well this are the instructions to my assignment
I tried using the way my teacher notes showed which included
but that never work and then I found the define function and that solved my problemCode:int array[10]= { 1, 2 ...} int array[12]= { 1, 2 ... 12}
Anyways this is my code.. The teacher said to use A, B, C as our integer names but I learned from here to use more meaningful names..hopefully I don't get knocked off points for using more then the 3 he said or for not using the names he asked for..
Code:#include <stdio.h> #define SIZE 10 #define SIZE1 12 int main() { int i; int j; int array1; int array2; int myArray[SIZE]; int myArray2[SIZE1]; int intersection[SIZE]; int match; for (i=0;i<SIZE;i++) { printf("Enter number: "); scanf("%d",&myArray[i]); } for(i=0;i<SIZE;i++) { printf("%d\n",myArray[i]); } for (i=0;i<SIZE1;i++) { printf("Enter number: "); scanf("%d",&myArray2[i]); } for(i=0;i<SIZE1;i++) { printf("%d\n",myArray2[i]); } match = 0; for(i=0; i<12; i++) { for(j=0; j<12; j++) { if (myArray[i] == myArray2[j]) { intersection[match] = myArray[i]; match++; } } } if (match>0) { printf("The intersection is: "); for(j=0; j<match; j++) printf("%d ",intersection[j]); } else printf("There is no intersection\n"); return 0; }
I also just realized I didn't use counter...damn.
Oh and here is to proof that it works
Also I have a very, very nooby question, I tend to think that my indentation is not the best. How do you guys indent one the source code starts to get huge?
I'm working on a reply to your previous post - I mention indentation.
Simple rule of thumb: every time you have code after an open brace '{', advance one tab stop. Every time you have a closing brace '}', go back one tab stop. Sometimes braces are optional - in that case, every time you have a line of code that depends on a control statement above it, indent that line of code.
This might not be applicable for every situation, but it's a good method to generally follow.Code://---------------- if(x == y) { code 1; // indent one level (underneath "if") code 2; // indent one level (underneath "if") } //---------------- if(a == b) code 1; // indent one level (underneath "if") //---------------- for(i=0; i<10; i++) { code 1; // indent one level (underneath "for") code 2; // indent one level (underneath "for") if(i < 6) // indent one level (underneath "for") code 3; // indent two levels (underneath "if", which is underneath "for") code 4; // indent one level (underneath "for") }
More here: SourceForge.net: Indentation - cpwiki
You have two defines of 10 and 12; I suggest using them.
Tim S.Code:for(i=0; i<12; i++) { for(j=0; j<12; j++) { if (myArray[i] == myArray2[j]) { intersection[match] = myArray[i]; match++; } } }
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
This makes me very happy.The teacher said to use A, B, C as our integer names but I learned from here to use more meaningful names
I made a few notes for you, and fixed the indentation - notice how it's easier to follow with the eyes.
There is one criteria of the assignment that is missing: "Repeated elements need to be counted only once. To do so, always check in C if the element to be inserted is already present."Code:#include <stdio.h> #define SIZE 10 #define SIZE1 12 int main() { int i; int j; int array1; int array2; int myArray[SIZE]; int myArray2[SIZE1]; int intersection[SIZE]; int match; for (i=0;i<SIZE;i++) { printf("Enter number: "); scanf("%d",&myArray[i]); } for(i=0;i<SIZE;i++) { printf("%d\n",myArray[i]); } for (i=0;i<SIZE1;i++) { printf("Enter number: "); scanf("%d",&myArray2[i]); } for(i=0;i<SIZE1;i++) { printf("%d\n",myArray2[i]); } match = 0; // Instead of the "magic number" 12, you should use your defined constant SIZE here. // Moreover, "i" controls "myArray", which only has 10 elements, so by using 12, // you're overrunning the bounds of your array for(i=0; i<12; i++) { // and here, you should be using SIZE1 instead of the magic number 12 for(j=0; j<12; j++) { if (myArray[i] == myArray2[j]) { intersection[match] = myArray[i]; match++; } } } if (match>0) { printf("The intersection is: "); for(j=0; j<match; j++) printf("%d ",intersection[j]); } else printf("There is no intersection\n"); return 0; }
Code:Enter number: 1 Enter number: 2 Enter number: 3 Enter number: 4 Enter number: 5 Enter number: 6 Enter number: 7 Enter number: 8 Enter number: 9 Enter number: 10 1 2 3 4 5 6 7 8 9 10 Enter number: 1 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 Enter number: 2 1 2 2 2 2 2 2 2 2 2 2 2 The intersection is: 1 2 2 2 2 2 2 2 2 2 2 2 2
Well I did some changes on my code simply since I found some dead weight int on it.
likeCode:Code:#include <stdio.h>#define SIZE 10 #define SIZE1 12 int main() { int i; int j; int A[SIZE]; int B[SIZE1]; int intersection[SIZE]; int counter=0; for (i=0;i<SIZE;i++) { printf("Enter number: "); scanf("%d",&A[i]); } for(i=0;i<SIZE;i++) { printf("%d\n",A[i]); } for (i=0;i<SIZE1;i++) { printf("Enter number: "); scanf("%d",&B[i]); } for(i=0;i<SIZE1;i++) { printf("%d\n",B[i]); } for(i=0; i<12; i++) { for(j=0; j<12; j++) { if (A[i] == B[j]) { intersection[counter] = A[i]; counter++; } } } if (counter>0) { printf("The intersection is: "); for(j=0; j<counter; j++) printf("%d ",intersection[j]); } else printf("There is no intersection\n"); return 0; }
Also regarding matrixes, which is what my next assignment is over. Are there any recommended reads over that? I tend to not be able to understand my teachers notes very well.Code:int array1 int array2
Thanks pal! So I did the changes to the names simply because the teacher is very picky regarding the naming. He wants us to use what he says so I tend to use real names or more useful names until it's time to turn it in.
Is that the only change you guys feel I should do to my assignment? I tend to ask questions on here a lot and this time I was determined to finish the assignment before asking on here.Code:#include <stdio.h>#define SIZE 10 #define SIZE1 12 int main() { int i; int j; int A[SIZE]; int B[SIZE1]; int intersection[SIZE]; int counter=0; for (i=0;i<SIZE;i++) { printf("Enter number: "); scanf("%d",&A[i]); } for(i=0;i<SIZE;i++) { printf("%d\n",A[i]); } for (i=0;i<SIZE1;i++) { printf("Enter number: "); scanf("%d",&B[i]); } for(i=0;i<SIZE1;i++) { printf("%d\n",B[i]); } for(i=0; i<SIZE; i++) { for(j=0; j<SIZE1; j++) { if (A[i] == B[j]) { intersection[counter] = A[i]; counter++; } } } if (counter>0) { printf("The intersection is: "); for(j=0; j<counter; j++) printf("%d ",intersection[j]); } else printf("There is no intersection\n"); return 0; }
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Huh after I change I did, using Size and Size 1 it now crashes at the end.
You can use:
It will work in the scope of where the array was defined.Code:#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))