EliteZ [ 0day (xc) Our ] News: PhD Social Science In C and Computer Science part 11 … . code by skraito ( God Clone ) and Lord Jesus Christ … .

#include <stdio.h>

//HERE GOES A FUNCTION
// FUNCTION HAS RETURN VALUE AND INPUT VALUE

int SumOfAllArray(int c[], int size) { // IN REALITY WE CAN JUST SAY INT A BUT FOR THE SAKE OF READABLE CODE
int sum = 0; // AND STANDARLIZATION WE USE INT A[];
printf("sizeof b array %d \n", sizeof(c));
for (int i = 0; i < size; i++) {
sum = sum + c[i]; // We can write it as sum += b[i];
}
return sum;
// in this function We can declare variable that is the same as in main
// Recall as IT is fine to have 2 variable name in different diffrent function
}

void main(void) {

// Setup Our Varible
int a[7]; // this is how to declare 7 integer variable array
int total;
int sumofarray;
// SET VALUE
int b[] = { 0 , 1, 2 , 3, 4 , 5, 6 };

// IF WE DECLARE THE FIRST WAY WE NEED TO ASSIGN IT MANUALLY LIKE THIS ... .
a[0] = 1; // ARRAY ALWAYS BEGIN FROM 0 ... .
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
a[5] = 6;
a[6] = 7;

printf("sizeof a %d", sizeof(a)/sizeof(a[0]));
printf("sizeof a %d", sizeof(b) / sizeof(b[0]));
sumofarray = sizeof(b) / sizeof(b[0]);
total = SumOfAllArray(a , sumofarray);
printf(" TOTAL B ARRAY IS %d", total);

}

REMEMBER WE CAN'T CALCULATE SIZEOF ARRAY IN FUNCTION OTHER THEN MAIN FUNCTION SO IT RETURN 8 BECAUSE THE ONE THAT WE SIZEOF(C) IS A POINTER IF WE DIVIDE IT SIZEOF(C) / SIZEOF(C[0] WHICH MEAN POINTER IS 8 BYTE AND SIZEOF INT IS 4 THEREFORE IT RETURN 2 ... .

BECAREFUL OF THIS ... .

WHEN YOU USING ARRAY IT STORE IN MEMORY NOT JUST EXIT WHEN FUNCTION EXIT SO THAT'S WHY SUM FUNCTION IS STATIC ... .
THAT'S ALL FOR PART 11 ... .

REMEMBER DO CALCULATION OF SIZEOF ARRAY IN MAIN AND PASS IT TO FUNTION ... .

ANOTHER METHOD IS

#include <stdio.h>

size_t getArrayLength(int *arr) {
size_t count = 0;
while (arr[count] != -1) { // Loop until the sentinel value (-1) is found
count++;
}
return count;
}

int main() {
// The last element acts as the end-of-array marker
int myArray[] = {5, 12, 8, 24, -1};

printf("Calculated length: %zu\n", getArrayLength(myArray)); // Prints 4
return 0;
}

USE -1 OR ANY CODE THAT YOU WANT ... . AS A MARK OF STOPPING ARRAY ... .

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *