Jump to content
Tilt

Problema vectori [bac]

Recommended Posts

Posted

Se consider? subprogramul sub, cu trei parametri:

· n, prin care prime?te un num?r natural (2<n<50);

· v, prin care prime?te un tablou unidimensional cu n elemente, numere naturale cu cel

mult 4 cifre;

· k, prin care prime?te un num?r natural (1<k?n).

Subprogramul returneaz? suma primelor k elemente cu valoare impar? ale tabloului. Dac?

nu exist? k elemente impare în tablou, subprogramul returneaz? valoarea -1.

Scrie?i defini?ia complet? a subprogramului.

Exemplu: dac? n=8, v=(2, 7, 6, 8, 3, 7, 5, 1), k=3, atunci subprogramul returneaz?

valoarea 17 (7+3+7=17).

Sunt curios daca am facut bine

int sub(unsigned int n, int v[50], unsigned int k) {
int OK=0, suma=0, i;
for (i=0; i<n; i++) {
if (v[i]%2==1 && OK!=k) {
OK=OK+1;
suma=suma+v[i];
}
}
if (OK==K)
return suma;
else return -1;
}

Posted

Toshiba, nu este corect. Se cer primele k numere impare din vector. Nu primele k numere. "Subprogramul returneaz? suma primelor k elemente cu valoare impar? ale tabloului"

Iata varianta mea

int sub(int n, int v[50], int k)

{

int counter = 0;

int sum = 0;

int i = 0;

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

{

if(v % 2 == 1)

{

sum += v;

counter++;

}

if(counter == k)

{

return sum;

}

}

return -1;

}

Posted

int sub(unsigned n, int v[50], unsigned k){

int ok=0, suma=0, i;

for(i=0;i<n-k;i++)

{

if(v%2!=0)

{

s+=v;

ok=1;

}

}

if(ok==1)

return s;

else

return -1;

}

Nu e bun ce ai f?cut.

Dac? un num?r impar se afl? exact la n-1 ce face programul t?u? :)) exact, nu-l bag?-n seam?. :))

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...