I dont understand where my error is coming from

Discussion in 'OT Technology' started by Danja, Jan 22, 2006.

  1. Danja

    Danja New Member

    Joined:
    Nov 16, 2005
    Messages:
    63
    Likes Received:
    0
    I am making a program to find the mean, median, and mode of a series of numbers in a file. I am getting one compiling error: G:\Program Files\DevStudio\MyProjects\bigproject\bigproject.cpp(120) : error C2664: 'strcpy' : cannot convert parameter 1 from 'int [10]' to 'char *'

    I'm confused. I dont have any chars in my program. Where is it getting it from? The error is in the body of my void bubblesort function on the line that I have marked with a "**".

    #include <iostream.h>
    #include <iomanip.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>

    void menu(int giraffe[], const int length);

    void mean (int sorted[], const int length);

    void median (int sorted[], const int length);

    void mode (int sorted[], const int length);

    int bubblesort (int giraffe[], const int length);

    void swap(int &x, int &y);

    int find_minimum(int giraffe[], int first, const int length);

    void printarray (int sorted[], const int length);

    int main()

    {
    const int length = 10;
    int giraffe[length];
    int sorted[10];

    ifstream in_file;

    in_file.open("info.doc");


    for (int x = 0; x < 10; x++)
    {

    in_file >> (giraffe[x]);

    }
    menu(giraffe, length);

    mean (sorted, length);

    median (sorted, length);

    mode (sorted, length);

    printarray (sorted, length);

    return 0;
    }

    void menu(int giraffe[], const int length)
    {
    cout << "menu" << endl;
    }

    void mean (int sorted[], const int length)

    {

    int index, sum=0;
    for(index=0; index < length; index++)
    {
    sum+= sorted[index];
    }
    cout << "The Mean Is: "<< (sum/length) << endl;
    }


    void median (int sorted[], const int length)

    {
    double med =0.0;
    if((length%2) == 0)
    {
    med = (((sorted[(length/2)] + sorted[((length/2) -1)])) /2.0);
    }

    else
    {
    med = sorted[length/2];
    }

    cout << "The Median Is: " << med << endl;
    }

    void mode (int sorted[], const int length)
    {
    int tempmode = 0, tempmode2 = 0, countmode = 0;

    for (int x = 0; x < 10; x++)
    {
    tempmode = sorted[x];

    for (int y = 0; y < 10; y++)

    if (tempmode == sorted[y])

    {
    countmode = tempmode;

    }
    }
    cout << countmode;
    }

    int bubblesort (int giraffe[], const int length)
    {
    int min_index = 0, sorted[10];

    for (int j = 0; j < length -1; ++j)
    {
    min_index = find_minimum(giraffe, j, length);
    if (min_index != j)
    swap(giraffe[j], giraffe[min_index]);
    }

    **strcpy (sorted,giraffe);

    return sorted[length];
    }


    int find_minimum(int giraffe[], int first, const int length)
    {
    int min_index = first;

    for (int j = first + 1; j < length; ++j)
    if (giraffe[j] < giraffe[min_index])
    min_index = j;
    return min_index;
    }

    void swap(int &x, int &y)
    {
    int temp =x;
    x = y;
    y = temp;
    }


    void printarray (int sorted[], const int length)
    {
    int count = 0;
    while (count < length)
    {
    cout << sorted[count] << endl;
    ++count;
    }
    cout << endl;
    }
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Please post your code inside a code block

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstdio>
    #include <string>
    
    The .h headers have been deprecated for quite a while now, use the correct names.
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    It looks like you are trying to use strcpy to copy contents of an array of ints. Strcpy is meant to be used with null terminated character arrays.

    Here is its function prototype:

    char * strcpy ( char * dest, const char * src );
    ( http://www.cplusplus.com/ref/cstring/strcpy.html )

    You can implement this yourself.

    Code:
    void copy(int source[], int destination[], int size)
    {
    	for (int i = 0; i < size; i++)
    		destination[i] = source[i];
    }
    
    A better way of doing this would be

    Code:
    
    //use  copy( (const int *)giraffe, sorted, length)  to call function
    
    void copy(const int source[], int destination[], int size)
    {
    	for (int i = 0; i < size; i++)
    		destination[i] = source[i];
    }
    
    The second piece of code does not allow you to accidentally change the source array.

    Also if you want to return an array from the bubblesort function, pass the array to the bubblesort function. Like in the copy function above. This will be using passing by reference.
     
    Last edited: Jan 22, 2006

Share This Page