Click here to Skip to main content
15,890,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 1:04
mybm130-Jul-14 1:04 
AnswerRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 1:07
mybm130-Jul-14 1:07 
GeneralRe: can anyone help how to find summation of float number? Pin
Richard MacCutchan30-Jul-14 2:22
mveRichard MacCutchan30-Jul-14 2:22 
GeneralRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 2:37
mybm130-Jul-14 2:37 
QuestionRe: can anyone help how to find summation of float number? Pin
CPallini29-Jul-14 21:32
mveCPallini29-Jul-14 21:32 
AnswerRe: can anyone help how to find summation of float number? Pin
mybm129-Jul-14 23:46
mybm129-Jul-14 23:46 
GeneralRe: can anyone help how to find summation of float number? Pin
CPallini30-Jul-14 1:09
mveCPallini30-Jul-14 1:09 
AnswerRe: can anyone help how to find summation of float number? Pin
enhzflep30-Jul-14 0:13
enhzflep30-Jul-14 0:13 
Consider the following implementation of a summation function and various ways of creating/populating an array for it to work on.

1. static data declared at compile time
2. dynamic data allocated with the c function malloc
3. dynamic data allocated with the c++ operator new

C++
#include <cstdlib>
#include <cstdio>

float sumArrayElements(float array[], int numItems)
{
    int i;
    float result = 0;
    for (i=0; i<numItems; i++)
        result += array[i];
    return result;
}

int main ()
{
    float array1[5] = {10.2, 13.7, 14.8, 99.0, 0.0};
    float arraySum1 = sumArrayElements( array1, 5);
    printf("Array sum: %.2f\n",arraySum1);

    int i;

    float *array2;
    int dynamicCount1 = 5;
    array2 = (float*)malloc(dynamicCount1 * sizeof(float) );
    for (i=0; i<dynamicCount1; i++)
        array2[i] = (float)rand()/(float)(RAND_MAX);
    float arraySum2 = sumArrayElements( array2, dynamicCount1);
    free(array2);
    printf("Array sum: %.2f\n",arraySum2);


    int dynamicCount2 = 10;
    float *array3 = new float[dynamicCount2];
    for (i=0; i<dynamicCount2; i++)
        array3[i] = (float)rand()/(float)(RAND_MAX);
    for (i=0; i<dynamicCount1; i++)
        array3[i] = (float)rand()/(float)(RAND_MAX);
    float arraySum3 = sumArrayElements( array3, dynamicCount2);
    delete array3;
    printf("Array sum: %.2f\n",arraySum3);

    return 0;
}

GeneralRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 0:36
mybm130-Jul-14 0:36 
GeneralRe: can anyone help how to find summation of float number? Pin
enhzflep30-Jul-14 1:07
enhzflep30-Jul-14 1:07 
GeneralRe: can anyone help how to find summation of float number? Pin
CPallini30-Jul-14 1:11
mveCPallini30-Jul-14 1:11 
GeneralRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 1:17
mybm130-Jul-14 1:17 
QuestionRe: can anyone help how to find summation of float number? Pin
CPallini30-Jul-14 2:00
mveCPallini30-Jul-14 2:00 
AnswerRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 2:32
mybm130-Jul-14 2:32 
GeneralRe: can anyone help how to find summation of float number? Pin
CPallini30-Jul-14 20:18
mveCPallini30-Jul-14 20:18 
GeneralRe: can anyone help how to find summation of float number? Pin
enhzflep30-Jul-14 1:18
enhzflep30-Jul-14 1:18 
GeneralRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 1:31
mybm130-Jul-14 1:31 
GeneralRe: can anyone help how to find summation of float number? Pin
enhzflep30-Jul-14 2:04
enhzflep30-Jul-14 2:04 
GeneralRe: can anyone help how to find summation of float number? Pin
mybm130-Jul-14 2:34
mybm130-Jul-14 2:34 
AnswerRe: can anyone help how to find summation of float number? Pin
Satya Chamakuri8-Aug-14 21:17
Satya Chamakuri8-Aug-14 21:17 
QuestionFortran 4 crash when call Write(*, *) Pin
Andraw11129-Jul-14 9:49
Andraw11129-Jul-14 9:49 
QuestionRe: Fortran 4 crash when call Write(*, *) Pin
David Crow29-Jul-14 14:20
David Crow29-Jul-14 14:20 
QuestionObject cloning Pin
Member 199339429-Jul-14 5:35
Member 199339429-Jul-14 5:35 
AnswerRe: Object cloning Pin
Shmuel Zang29-Jul-14 19:54
Shmuel Zang29-Jul-14 19:54 
JokeRe: Object cloning Pin
«_Superman_»29-Jul-14 20:03
professional«_Superman_»29-Jul-14 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.