How to Iterate through arrays in C? - arrays

So I need to fill in the code for the program to work for the question:
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.
#include <stdio.h>
int main(void) {
const int NUM_VALS = 4;
int testGrades[NUM_VALS];
int i = 0;
int sumExtra = -9999; // Initialize to 0 before your for loop
testGrades[0] = 101;
testGrades[1] = 83;
testGrades[2] = 107;
testGrades[3] = 90;
// STUDENT CODE GOES HERE
return 0;
}
So far all I have is:
for (i=0;i<NUM_VALS;++i) {
if (testGrades[i] > 100) {
sumExtra = testGrades[i] - 100;
}
}
I dont know how to find the sum of the array of the values over 100.

Firstly, initialise sumExtra to 0. Then just change:
sumExtra = testGrades[i] - 100;
to
sumExtra += testGrades[i] - 100;
because the sumExtra for a particular index i is testGrades[i]-100, and you want to find the total of the sumExtra, and hence keeping adding this to the sumExtra variable.

You are missing the if statement that checks if the test score is above 100. Here is the code that works:
sumExtra = 0;
for (i = 0; i < NUM_VALS; ++i) {
if (testGrades[i] >= 101) {
sumExtra += (testGrades[i] - 100);
}
}
cout << "sumExtra: " << sumExtra << endl;

Initialize to 0 before your for loop
int sumExtra = 0;
sumExtra = testGrades[i]-100;
sumExtra += testGrades[i]-100;

Related

Is this a good setup for a probability based random number generator for C?

In my opinion, I feel like this is definitely not achieving what it's supposed to do. I was needing a random number generator based off probability to determine a winner of a race. So Runner A has a 40% chance of winning, for example.
//function definition
void createDogs(DOGS* dogList) {
//adding info the the dogInfo struct
strcpy(dogList[0].dogName, "Easy Rex"); //dog 1
dogList[0].odds = 40;
dogList[0].payoutMultiplier = 2;
strcpy(dogList[1].dogName, "Worried Bud"); //dog 2
dogList[1].odds = 10;
dogList[1].payoutMultiplier = 5;
strcpy(dogList[2].dogName, "Money Ace"); //dog 3
dogList[2].odds = 8;
dogList[2].payoutMultiplier = 10;
strcpy(dogList[3].dogName, "Lucky Lady"); //dog 4
dogList[3].odds = 15;
dogList[3].payoutMultiplier = 15;
strcpy(dogList[4].dogName, "Cash Dawg"); //dog 5
dogList[4].odds = 1;
dogList[4].payoutMultiplier = 50;
strcpy(dogList[5].dogName, "Unlucky Brutus"); //dog 6
dogList[5].odds = 4;
dogList[5].payoutMultiplier = 20;
strcpy(dogList[6].dogName, "Gamble Champ"); //dog 7
dogList[6].odds = 8;
dogList[6].payoutMultiplier = 10;
strcpy(dogList[7].dogName, "Nothing Chewy"); //dog 8
dogList[7].odds = 10;
dogList[7].payoutMultiplier = 5;
strcpy(dogList[8].dogName, "Easy Roxy"); //dog 9
dogList[8].odds = 13;
dogList[8].payoutMultiplier = 3;
}//end createDogs
So here's where I put the probability, under the "odds", then here is where I implement it. Thinking that the odds is the percentage so grabbing the random number from 0 to that "percentage" and then compare them with the others to determine the winner.
//function definition
void dogRace(DOGS* dogList, DATA* raceInfo, int counter) {
int numberRolled[NO_OF_DOGS];
int i, moneyWon;
int biggestNumber, position = 0;
srand(time(0));
printf("\nAnd the race is on!");
pause(5);
for (i = 0; i < NO_OF_DOGS; i++) { //assigns a number to each dog based on its odds
numberRolled[i] = (rand() % dogList[i].odds);
}//end for
biggestNumber = numberRolled[0];
for (i = 0; i < NO_OF_DOGS; i++) { //determines which dog won (>number rolled)
if (biggestNumber < numberRolled[i]) {
biggestNumber = numberRolled[i];
position = i;
}//end if
}//end for
}
Technically it works, but I feel as it's not really the right way to do it- let me know if there is a better way to do this because my textbooks do no give any example for this type of problem. I've also tried googling/youtubing it and had no luck finding for what I was looking for.
Try using cumulative odds:
S = sum of odds of dogs 0 to number of dogs - 1
R = random integer from 0 to S-1
i = 0
while i < number of dogs and dog[i].odds <= R: R = R - dog[i].odds, i = i + 1
//function definition
void dogRace(DOGS* dogList, DATA* raceInfo, int counter) {
int i, moneyWon, position;
int sum_of_odds = 0;
int rolled;
for (i = 0; i < NO_OF_DOGS; i++) {
sum_of_odds += dogList[i].odds;
}
// Note, usually you should only call srand() once in a program.
srand(time(0));
printf("\nAnd the race is on!");
pause(5);
rolled = randInt(sum_of_odds);
for (i = 0; i < NO_OF_DOGS; i++) {
if (dogList[i].odds > rolled)
break;
rolled -= dogList[i].odds;
}//end for
position = i;
}
The randInt function called above is given below:
// random integer from 0 to n-1 (for n in range 1 to RAND_MAX+1u)
int randInt(unsigned int n) {
unsigned int x = (RAND_MAX + 1u) / n;
unsigned int limit = x * n;
int s;
do {
s = rand();
} while (s >= limit);
return s / x;
}
The above is preferable to using rand() % n because it removes any bias in the likely case that RAND_MAX+1 is not a multiple of n. Also, some implementations of rand() produce not very random sequences for rand() % n so it is better to use the quotient of division rather than the remainder.

C - getting prime numbers using this algorithm

I am fighting some simple question.
I want to get prime numbers
I will use this algorithm
and... I finished code writing like this.
int k = 0, x = 1, n, prim, lim = 1;
int p[100000];
int xCount=0, limCount=0, kCount=0;
p[0] = 2;
scanf("%d", &n);
start = clock();
do
{
x += 2; xCount++;
if (sqrt(p[lim]) <= x)
{
lim++; limCount++;
}
k = 2; prim = true;
while (prim && k<lim)
{
if (x % p[k] == 0)
prim = false;
k++; kCount++;
}
if (prim == true)
{
p[lim] = x;
printf("prime number : %d\n", p[lim]);
}
} while (k<n);
I want to check how much repeat this code (x+=2; lim++; k++;)
so I used xCount, limCount, kCount variables.
when input(n) is 10, the results are x : 14, lim : 9, k : 43. wrong answer.
answer is (14,3,13).
Did I write code not well?
tell me correct point plz...
If you want to adapt an algorithm to your needs, it's always a good idea to implement it verbatim first, especially if you have pseudocode that is detailed enough to allow for such a verbatim translation into C-code (even more so with Fortran but I digress)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main (void){
// type index 1..n
int index;
// var
// x: integer
int x;
//i, k, lim: integer
int i, k, lim;
// prim: boolean
bool prim;
// p: array[index] of integer {p[i] = i'th prime number}
/*
We cannot do that directly, we need to know the value of "index" first
*/
int res;
res = scanf("%d", &index);
if(res != 1 || index < 1){
fprintf(stderr,"Only integral values >= 1, please. Thank you.\n");
return EXIT_FAILURE;
}
/*
The array from the pseudocode is a one-based array, take care
*/
int p[index + 1];
// initialize the whole array with distinguishable values in case of debugging
for(i = 0;i<index;i++){
p[i] = -i;
}
/*
Your variables
*/
int lim_count = 0, k_count = 0;
// begin
// p[1] = 2
p[1] = 2;
// write(2)
puts("2");
// x = 1
x = 1;
// lim = 1
lim = 1;
// for i:=2 to n do
for(i = 2;i < index; i++){
// repeat (until prim)
do {
// x = x + 2
x += 2;
// if(sqr(p[lim]) <= x) then
if(p[lim] * p[lim] <= x){
// lim = lim +1
lim++;
lim_count++;
}
// k = 2
k = 2;
// prim = true
prim = true;
// while (prim and (k < lim)) do
while (prim && (k < lim)){
// prim = "x is not divisible by p[k]"
if((x % p[k]) == 0){
prim = false;
}
// k = k + 1
k++;
k_count++;
}
// (repeat) until prim
} while(!prim);
// p[i] := x
p[i] = x;
// write(x)
printf("%d\n",x);
}
// end
printf("x = %d, lim_count = %d, k_count = %d \n",x,lim_count,k_count);
for(i = 0;i<index;i++){
printf("%d, ",p[i]);
}
putchar('\n');
return EXIT_SUCCESS;
}
It will print an index - 1 number of primes starting at 2.
You can easily change it now--for example: print only the primes up to index instead of index - 1 primes.
In your case the numbers for all six primes up to 13 gives
x = 13, lim_count = 2, k_count = 3
which is distinctly different from the result you want.
Your translation looks very sloppy.
for i:= 2 to n do begin
must translate to:
for (i=2; i<=n; i++)
repeat
....
until prim
must translate to:
do {
...
} while (!prim);
The while prim... loop is inside the repeat...until prim loop.
I leave it to you to apply this to your code and to check that all constructs have been properly translated. it doesn't look too difficult to do that correctly.
Note: it looks like the algorithm uses 1-based arrays whereas C uses 0-based arrays.

how can i make this code more efficient? need to calaulate number of combinations

#include<stdio.h>
void main() {
int nis1 = 0, nis2 = 0, nis5 = 0, nis10 = 0, nis20 = 0, nis50 = 0, nis100 = 0, nis200 = 0;
long int num, count = 0, sum = 0;
while (1)
{
printf_s("what number you like to check? (or press '0' to exit)\n");
scanf_s("%d", &num);
if (num == 0)
break;
for (nis200 = 0; nis200 <= num / 200; nis200++) {
for (nis100 = 0; nis100 <= num / 100; nis100++) {
for (nis50 = 0; nis50 <= num / 50; nis50++) {
for (nis20 = 0; nis20 <= num / 20; nis20++) {
for (nis10 = 0; nis10 <= num / 10; nis10++) {
for (nis5 = 0; nis5 <= num / 5; nis5++) {
for (nis2 = 0; nis2 <= num / 2; nis2++) {
for (nis1 = 0; nis1 <= num; nis1++) {
sum = nis200 * 200 + nis100 * 100 + nis50 * 50 + nis20 * 20 + nis10 * 10 + nis5 * 5 + nis2 * 2 + nis1 * 1;
if (sum == num) {
count++;
break;
}
if (sum > num)
{
break;
}
}
}
}
}
}
}
}
}
printf_s("the number of combinations is: %d\n", count);
count = 0;
}
}
//i have to build a code that with a given number by the user, how many posibillities are there to sum the number with the number:1,2,5,10,20,50,100,200.
Check in each for loop if your sum is already above the num and break the loop if it is the case. Not the ultimate optimization but it makes your code much faster.
You do a lot of counting from 0 to whatever. If you use a number like 500, then your first iteration will be 0 x 200, 0 x 100 ... all the way down to 0 x 2, and then you count 1s from 0 to 500. When you're down to your last option, you should be able to calculate how many 1s you still need.
For any given bill, you always count how many of it you need without regard for the bills you've already selected. If you use a number like 500, and you already have 2 x 200, then you will never need 100 5s; you should be able to calculate the maximum number of 5s you will need. (Keep a running total of each selection-so-far).

Can help me with my C code?

I'm supposed to make a program which allows me to do this calculation:
-5+10-20+40-80+160
I've done this so far:
const int START = -5, LIMIT = 160;
int somme = 0;
int terme = START;
do {
somme += terme;
terme = terme * 2;
terme = -terme;
} while(terme <= LIMIT);
printf ("equals %d\n\n", somme);
But when I run it it shows -215 and of course it's not the correct answer. I'd really appreciate your help.
You should use absulute value of terme in condition of your loop, that better have to be PRE-CONDITION while:
#include <stdio.h>
#define ABS(X) (X>=0)?(X):(-X)
int main()
{
const int START = -5, LIMIT = 160;
int somme = 0;
int terme = START;
while( ABS(terme) <= LIMIT )
{
somme += terme;
terme = terme * 2;
terme = -terme;
}
printf ("equals %d\n\n", somme);
}
For a calculation like this, you should look at the expression more carefully. What you want is:
-5+10-20+40-80+160
= 5*(- 1 + 2 - 4 + 8 - 16 + 32)
= 5*( (-1)^1*(2^0) + (-1)^1*(2^1) + (-1)^1*(2^2) + (-1)^1*(2^3) + (-1)^1*(2^4) + (-1)^1*(2^5) )
where in C terms, a^b is equivalent to pow(a,b)
= 5 * sum over i ((-1)^(i+1) * 2^i ) where i goes from 0 to 5
Do you think that it would be easier to iterate over a variable i in a for loop? Ill leave this as an exercise.
another cool way:
const int START = -5, LIMIT = 160;
int somme = 0;
for(int terme = START; (terme<LIMIT && (-terme)<LIMIT)||(printf("equals //
%d\n\n",somme),0); terme = -(terme*2)){
somme += terme;
}

Distribute elements between equivalent arrays to achieve balanced sums

I am given a set of elements from, say, 10 to 21 (always sequential),
I generate arrays of the same size, where size is determined runtime.
Example of 3 generated arrays (arrays # is dynamic as well as # of elements in all arrays, where some elements can be 0s - not used):
A1 = [10, 11, 12, 13]
A2 = [14, 15, 16, 17]
A3 = [18, 19, 20, 21]
these generated arrays will be given to different processes to to do some computations on the elements. My aim is to balance the load for every process that will get an array. What I mean is:
With given example, there are
A1 = 46
A2 = 62
A3 = 78
potential iterations over elements given for each thread.
I want to rearrange initial arrays to give equal amount of work for each process, so for example:
A1 = [21, 11, 12, 13] = 57
A2 = [14, 15, 16, 17] = 62
A3 = [18, 19, 20, 10] = 67
(Not an equal distribution, but more fair than initial). Distributions can be different, as long as they approach some optimal distribution and are better than the worst (initial) case of 1st and last arrays. As I see it, different distributions can be achieved using different indexing [where the split of arrays is made {can be uneven}]
This works fine for given example, but there may be weird cases..
So, I see this as a reflection problem (due to the lack of knowledge of proper definition), where arrays should be seen with a diagonal through them, like:
10|111213
1415|1617
181920|21
And then an obvious substitution can be done..
I tried to implement like:
if(rest == 0)
payload_size = (upper-lower)/(processes-1);
else
payload_size = (upper-lower)/(processes-1) + 1;
//printf("payload size: %d\n", payload_size);
long payload[payload_size];
int m = 0;
int k = payload_size/2;
int added = 0; //track what been added so far (to skip over already added elements)
int added2 = 0; // same as 'added'
int p = 0;
for (i = lower; i <= upper; i=i+payload_size){
for(j = i; j<(i+payload_size); j++){
if(j <= upper){
if((j-i) > k){
if(added2 > j){
added = j;
payload[(j-i)] = j;
printf("1 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}else{
printf("else..\n");
}
}else{
if(added < upper - (m+1)){
payload[(j-i)] = upper - (p*payload_size) - (m++);
added2 = payload[(j-i)];
printf("2 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}else{
payload[(j-i)] = j;
printf("2.5 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}
}
}else{ payload[(j-i)] = '\0'; }
}
p++;
k=k/2;
//printf("send to proc: %d\n", ((i)/payload_size)%(processes-1)+1);
}
..but failed horribly.
You definitely can see the problem in the implementation, because it is poorly scalable, not complete, messy, badly written and so on, and on, and on, ...
So, I need help either with the implementation or with an idea of a better approach to do what I want to achieve, given the description.
P.S. I need the solution to be as 'in-liney' as possible (avoid loop nesting) - that is why I am using bunch of flags and global indexes.
Surely this can be done with extra loops and unnecessary iterations. I invite people that can and appreciate t̲h̲e̲ ̲a̲r̲t̲ ̲o̲f̲ ̲i̲n̲d̲e̲x̲i̲n̲g̲ when it comes to arrays.
I am sure there is a solution somewhere out there, but I just cannot make an appropriate Google query to find it.
Hint? I thought of using index % size_of_my_data to achieve this task..
P.S. Application: described here
Here is an O(n) solution I wrote using deque (double-ended queue, a deque is not necessary and a simple array can be used, but a deque makes the code clean because of popRight and popLeft). The code is Python, not pseudocode, but it should be pretty to understand (because it's Python).:
def balancingSumProblem(seqStart = None, seqStop = None, numberOfArrays = None):
from random import randint
from collections import deque
seq = deque(xrange(seqStart or randint(1, 10),
seqStop and seqStop + 1 or randint(11,30)))
arrays = [[] for _ in xrange(numberOfArrays or randint(1,6))]
print "# of elements: {}".format(len(seq))
print "# of arrays: {}".format(len(arrays))
averageNumElements = float(len(seq)) / len(arrays)
print "average number of elements per array: {}".format(averageNumElements)
oddIteration = True
try:
while seq:
for array in arrays:
if len(array) < averageNumElements and oddIteration:
array.append(seq.pop()) # pop() is like popright()
elif len(array) < averageNumElements:
array.append(seq.popleft())
oddIteration = not oddIteration
except IndexError:
pass
print arrays
print [sum(array) for array in arrays]
balancingSumProblem(10,21,3) # Given Example
print "\n---------\n"
balancingSumProblem() # Randomized Test
Basically, from iteration to iteration, it alternates between grabbing large elements and distributing them evenly in the arrays and grabbing small elements and distributing them evenly in the arrays. It goes from out to in (though you could go from in to out) and tries to use what should be the average number of elements per array to balance it out further.
It's not 100 percent accurate with all tests but it does a good job with most randomized tests. You can try running the code here: http://repl.it/cJg
With a simple sequence to assign, you can just iteratively add the min and max elements to each list in turn. There are some termination details to fix up, but that's the general idea. Applied to your example the output would look like:
john-schultzs-macbook-pro:~ jschultz$ ./a.out
10 21 13 18 = 62
11 20 14 17 = 62
12 19 15 16 = 62
A simple reflection assignment like this will be optimal when num_procs evenly divides num_elems. It will be sub-optimal, but still decent, when it doesn't:
#include <stdio.h>
int compute_dist(int lower, int upper, int num_procs)
{
if (lower > upper || num_procs <= 0)
return -1;
int num_elems = upper - lower + 1;
int num_elems_per_proc_floor = num_elems / num_procs;
int num_elems_per_proc_ceil = num_elems_per_proc_floor + (num_elems % num_procs != 0);
int procs[num_procs][num_elems_per_proc_ceil];
int i, j, sum;
// assign pairs of (lower, upper) to each process until we can't anymore
for (i = 0; i + 2 <= num_elems_per_proc_floor; i += 2)
for (j = 0; j < num_procs; ++j)
{
procs[j][i] = lower++;
procs[j][i+1] = upper--;
}
// handle left overs similarly to the above
// NOTE: actually you could use just this loop alone if you set i = 0 here, but the above loop is more understandable
for (; i < num_elems_per_proc_ceil; ++i)
for (j = 0; j < num_procs; ++j)
if (lower <= upper)
procs[j][i] = ((0 == i % 2) ? lower++ : upper--);
else
procs[j][i] = 0;
// print assignment results
for (j = 0; j < num_procs; ++j)
{
for (i = 0, sum = 0; i < num_elems_per_proc_ceil; ++i)
{
printf("%d ", procs[j][i]);
sum += procs[j][i];
}
printf(" = %d\n", sum);
}
return 0;
}
int main()
{
compute_dist(10, 21, 3);
return 0;
}
I have used this implementation, which I mentioned in this report (Implementation works for cases I've used for testing (1-15K) (1-30K) and (1-100K) datasets. I am not saying that it will be valid for all the cases):
int aFunction(long lower, long upper, int payload_size, int processes)
{
long result, i, j;
MPI_Status status;
long payload[payload_size];
int m = 0;
int k = (payload_size/2)+(payload_size%2)+1;
int lastAdded1 = 0;
int lastAdded2 = 0;
int p = 0;
int substituted = 0;
int allowUpdate = 1;
int s;
int times = 1;
int times2 = 0;
for (i = lower; i <= upper; i=i+payload_size){
for(j = i; j<(i+payload_size); j++){
if(j <= upper){
if(k != 0){
if((j-i) >= k){
payload[(j-i)] = j- (m);
lastAdded2 = payload[(j-i)];
}else{
payload[(j-i)] = upper - (p*payload_size) - (m++) + (p*payload_size);
if(allowUpdate){
lastAdded1 = payload[(j-i)];
allowUpdate = 0;
}
}
}else{
int n;
int from = lastAdded1 > lastAdded2 ? lastAdded2 : lastAdded1;
from = from + 1;
int to = lastAdded1 > lastAdded2 ? lastAdded1 : lastAdded2;
int tempFrom = (to-from)/payload_size + ((to-from)%payload_size>0 ? 1 : 0);
for(s = 0; s < tempFrom; s++){
int restIndex = -1;
for(n = from; n < from+payload_size; n++){
restIndex = restIndex + 1;
payload[restIndex] = '\0';
if(n < to && n >= from){
payload[restIndex] = n;
}else{
payload[restIndex] = '\0';
}
}
from = from + payload_size;
}
return 0;
}
}else{ payload[(j-i)] = '\0'; }
}
p++;
k=(k/2)+(k%2)+1;
allowUpdate = 1;
}
return 0;
}

Resources