Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (shayaan_mustafa):

Write a program that reads 8 floats into an array and then prints out the second, fourth, sixth and eighth members of the array, and the sum of the first, third, fifth and seventh members of the array.

OpenStudy (anonymous):

What have you got so far?

OpenStudy (anonymous):

Also, what language are you using :-D you can paste your code in a site like http://ideone.com

OpenStudy (anonymous):

[If he shows us what he got so far, I was assuming we could deduce the language from that :-)]

OpenStudy (shayaan_mustafa):

OK. Its not my assignment. Its my friend Alex's assignment. Due to electricity shortfall he has lost his PC (In PC he had saved his assignment) and after 3 hours he has to submit his assignment. He asked me that if there anyone who could volunteerily make at least 2 programs for him. So I said yes may be experts on openstudy could help you. It is Turbo C Honestly I know C language. but till functions. not array.. Kindly people tell me what should I do now? :(

OpenStudy (anonymous):

unlucky guy :( To begin, you can declare a float array of size 8. Then, you can use a for loop, using the scanf() function to read floating point values one-by-one into the array. For the last two (first printing even elements, and then printing odd elements), you can use two more for loops to read values from the array.

OpenStudy (shayaan_mustafa):

If I try program here. then could you help me step by step @agdgdgdgwngo ? I will appreciate your kindness for me.

OpenStudy (anonymous):

sure

OpenStudy (shayaan_mustafa):

Thanks

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { float my [] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8} Is this declaration is right?

OpenStudy (anonymous):

it would work, but since you are reading values from stdin you don't really have to fill the array with {1,2,3,4,5,6,7,8}. you can use an empty array instead, like: float my[8];

OpenStudy (shayaan_mustafa):

ok you mean like this. #include<stdio.h> #include<conio.h> void main(void) { float my [8];

OpenStudy (anonymous):

right! alright that's part 1 of the problem. now you need to read floats from input.

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { clrscr(); float my[8] for(int i=0;i<8;i++) scanf("%f",my[i]); getch(); } Is it right? I have taken input.

OpenStudy (anonymous):

it's almost right. you just need to watch out at scanf. :) the second argument to scanf must be a pointer. instead you are using a float. you must do some pointer arithmetic to get the i'th address of your float array. You can do scanf("%f", my + i); or you can do scanf("%f", &(my[i]));

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { clrscr(); float my[8]; for(int i=0;i<8;i++) scanf("%f",&my[i]); getch(); } Is it right ? Now I would have ith address.

OpenStudy (shayaan_mustafa):

it was 2nd step. But I am confused at 3rd step, what you said about even and odd :(

OpenStudy (anonymous):

he abstracted from "second, fourth, sixth and eighth" and "first, third, fifth and seventh "

OpenStudy (anonymous):

First, the 2nd, 4th, 6th, and 8th items of the array need to be printed out. You need to loop the array from i = 1 (the second item) to i = 7 (the last item), incrementing i by 2. something like for (i = 1; i < 8; i += 2) { /* print out the ith element of the array */ }

OpenStudy (shayaan_mustafa):

@nczempin yes I know about even and odd. but how to do this in array.. kindly give some hint.

OpenStudy (anonymous):

just one additional for loop would suffice. You can do the printing and the summing within the 1 loop

OpenStudy (shayaan_mustafa):

OK now I try as agdgdgdgwngo says.

OpenStudy (anonymous):

for (int i =0; i<8; i++){ //sorry, Java programmer if (i%2==0){ // i is even, so the position within the array is odd //do your printing } else{ //do your summing } }

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { clrscr(); float my[8]; for(int i=0;i<8;i++) { scanf("%f",&my[i]); } for(i=1;i<8;i+=2) { printf("%f",my[i]) } getch(); } Fine?

OpenStudy (anonymous):

right; that prints out the even-indexed elements. now print out the odd ones (first, third, fifth, seventh)

OpenStudy (shayaan_mustafa):

@agdgdgdgwngo we are using here two for loops. and nczempin used one. So What you think Which one is easy. I am asking because you are expert people in languages..

OpenStudy (anonymous):

they both work; whatever you find easier to understand

OpenStudy (anonymous):

my bad :-P I got it wrong do it nczempin's way; you are supposed to sum the odd ones :-P

OpenStudy (anonymous):

The % operator may not be on everyone's radar ;-)

OpenStudy (shayaan_mustafa):

Its OK agdgdgdgwngo.. But be here. I need both of you experts. Now I am trying nczempin's way.

OpenStudy (anonymous):

well, just change "now print the odd ones" to "now sum the odd ones"

OpenStudy (anonymous):

lol. standing by, at your service :-)

OpenStudy (anonymous):

void main() is not standard.

OpenStudy (anonymous):

Please don't use it.

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { clrscr(); float my[8]; for(int i=0;i<8;i++) scanf("%f",my[i]); for (int i =0; i<8; i++) if (i%2==0) printf("%f",my[i]); else HOW TWO SUM ARRAY ELEMENTS HERE? getch(); } Kindly look remaining code is fine?

OpenStudy (shayaan_mustafa):

FFM so how?

OpenStudy (anonymous):

scanf("%f",my[i]); --> Last time I check if it's not a character array or pointer it needs '&'

OpenStudy (shayaan_mustafa):

@FFM sorry friend I forgot & sign. But look remaining code. except else body.

OpenStudy (anonymous):

Google "void main() vs int main()". main() returns an integer (always).

OpenStudy (anonymous):

for the sum, you can declare a `sum` variable before the loop and set it to zero. float sum = 0; and in the part of the loop where you add to the sum, you can have something like sum += my[i]; so at the very end of the program you can print out the sum using printf

OpenStudy (shayaan_mustafa):

#include<stdio.h> #include<conio.h> void main(void) { clrscr(); float my[8]sum=0; for(int i=0;i<8;i++) scanf("%f",&my[i]); for (int i =0; i<8; i++) if (i%2==0) printf("%f",a[i]); else sum+=my[i]; printf("print sum %f",m[i]); getch(); } Fine agdgdgdgwngo? Is it right now?

OpenStudy (shayaan_mustafa):

@FFM I have gotten what you have said about main() function. and in the last i will return 0

OpenStudy (anonymous):

int main(){ float Sum = 0.0, F[8]; int i; //Reading the inputs: for( i=1; i<= 8;i++) scanf("%f", &F[i-1]); printf(" The Even terms are:\n"); for( i=1; i<= 8;i++){ if(i &1) Sum += F[i-1]; else printf("%f ",F[i-1]); } printf("\n Sum of Odd terms: %f\n",Sum); return 0; } I haven't debugged but this should work!

OpenStudy (shayaan_mustafa):

Thanks a lot FFM but I want you to describe lines. scanf("%f", &F[i-1]); i-1 implies what?

OpenStudy (anonymous):

See I have set i from 1 to n but the array is starting from 0 so just memory optimization.

OpenStudy (anonymous):

it implies that what we call the "first" element is really the "zeroth" in C, etc. I had to make a similar adjustment in my pseudocode.

OpenStudy (anonymous):

@Shayaan http://ideone.com/G15BY you just need to clean up the syntax and then it should compile and run since we are doing the right steps let's see if fffm's code works: http://ideone.com/l7VOX yeah it works correctly

OpenStudy (anonymous):

and i&1 is one step above even the % operator ;-)

OpenStudy (anonymous):

bitwise operations ftw

OpenStudy (anonymous):

it may be a little suspicous if you're using bitwise and or modulo but have never learned it :)

OpenStudy (shayaan_mustafa):

One last thing @agdgdgdgwngo how did you compile the code?

OpenStudy (anonymous):

so an "easier" way of doing it (checking even vs. odd) would be to divide by two and multiply back by two; if it's the same number as the original, the original was even. So come to think of it, just doing an additional for loop, while not as elegant, is probably easier to understand for a beginner.

OpenStudy (anonymous):

Or even easier would be sum = my[0]+my[2]+my[4] etc.

OpenStudy (anonymous):

for size 8 that's fine, it would just get a little tedious for 800 :-)

OpenStudy (anonymous):

I just used http://ideone.com to build and run the code ideone.com expects plain C thogh, so if you remove the #inlude <conio.h> and the corresponding functions (getch() and clrsr()) your code should run as well.

OpenStudy (shayaan_mustafa):

thanks all 3 of you. Even I don't know how to work with arrays but you guys are so helpful as I said to my friend. Thanks @agdgdgdgwngo @nczempin @FoolForMath

OpenStudy (shayaan_mustafa):

Now I have one medal so whom should I give it.. hmm... I suggest not to give it to anyone of you because I all 3 equally helped me. Should I give it or not friends?

OpenStudy (shayaan_mustafa):

I think it is given to foolformath because you both have already one :D lol

OpenStudy (anonymous):

yea

OpenStudy (anonymous):

that's fine

OpenStudy (shayaan_mustafa):

But I want you people always be there for my help where I need it. OK guys?

OpenStudy (shayaan_mustafa):

It is first program that I have made with the help of you experts. Thanks once again.

OpenStudy (anonymous):

sry, can't promise ;-)

OpenStudy (shayaan_mustafa):

I know you can't promise. But you can try atleast.

OpenStudy (anonymous):

just one request: Please _always_ put brackets around "if clauses". Trust me, you'll thank me eventually

OpenStudy (anonymous):

and always follow foolformath's suggestion use int main! :-D (...well, except for a few VERY special cases)

OpenStudy (shayaan_mustafa):

@nczempin OK I will consider this for next time. You will not see again without brackets. thnx for this.

OpenStudy (shayaan_mustafa):

@agdgdgdgwngo yes. I wil follow his suggestion. Thnx

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!