logo
down
shadow

Trouble with array in function


Trouble with array in function

By : user4580059
Date : November 28 2020, 09:01 AM
This might help you You need to pass your array rowavg[] or make it global.
Here is some correction:
code :
#include <stdio.h>

void row_average(int vals2d[][4], int rows, int cols, float rowavg[])
{
    int k, l, sum;
    float avg;
    for (k=0; k<rows; k++){
        sum = 0;
        avg = 0;
        for (l=0; l<cols; l++){
            sum = sum + vals2d[k][l];
            avg = (float)sum/4;
        }
        rowavg[k] = avg;
        printf("%.2f  ", rowavg[k]);
    }
}

void array_1display(float rowavg[], int size)
{
    int m;
    for (m=0; m<size; m++){
        printf("%.3f  ", rowavg[m]);
    }
}

int main()
{
    float rowavg[6];

    int vals2d[6][4] ={
            {12,54,34,45},
            {68,76,65,23},
            {75,23,76,34},
            {6,45,58,59},
            {35,67,93,12},
            {66,90,25,7}
        };

    row_average(vals2d, 6, 4, rowavg);
    printf("\n");
    array_1display(rowavg, 6);
}


Share : facebook icon twitter icon
Having trouble passing array to function

Having trouble passing array to function


By : Lucas Carmona Ampuer
Date : March 29 2020, 07:55 AM
it fixes the issue This looks like a school assignment and I applaud you for not asking for the answer. There are several ways to do it, but you are already fairly close in the approach that you are using. When you pass an array reference, you do not want to include the length of the array. For example, the parameter int score[100] should be int score[]. The exception, especially in your scenario, is with multidimensional arrays. In this case, you want to use char playerName[][20]. Your function declaration also needs to change to match. Don't forget InputData returns an int. Your declarations and function call are correct; you just need to adjust your function signature.
Array and function trouble

Array and function trouble


By : Ronald
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You are setting the value of the string only at the start of your main routine. You need to set it after the ThreeResults are set. Strings are immutable objects, so your code would not work.
code :
Module Module1

Sub Main()
    'Declare Variables
    Dim UserAnswer As String = "nothing"
    Dim UserTokens As String
    Dim ThreeResults(2) As String
    Dim Result1 As String
    Dim Result2 As String
    Dim Result3 As String

    'Ask user for number of tokens
    Console.WriteLine("Enter the number of tokens that you want to gamble with (minimum of 1 token)")
    UserTokens = (Console.ReadLine())

    UserTokens = ValidateTokens(UserTokens)

    Do Until UserAnswer = "no"
        ThreeResults = GenerateNumbers()
        Result1 = ThreeResults(0)
        Result2 = ThreeResults(1)
        Result3 = ThreeResults(2)

        UserAnswer = DisplayResults(UserTokens, UserAnswer, Result1, Result2, Result3)
    Loop

End Sub

Function ValidateTokens(ByVal UserTokens)

    Dim dblUserTokens As Double

    Do Until IsNumeric(UserTokens)
        Console.Write("Sorry you did not enter a valid number.  Please try again.   ")
        UserTokens = Console.ReadLine()
    Loop

    'Make sure the user entered in a positive number
    dblUserTokens = CDbl(UserTokens)
    Do Until dblUserTokens > 0
        Console.Write("Sorry you did not enter in a positive number.  Please try again.   ")
        UserTokens = Console.ReadLine()

        'Make sure the user entered in a number
        Do Until IsNumeric(UserTokens)
            Console.Write("Sorry you did not enter a valid number.  Please try again.   ")
            UserTokens = Console.ReadLine()
        Loop
        dblUserTokens = CDbl(UserTokens)


    Loop

    Return UserTokens
End Function

Function GenerateNumbers()
    Dim results(2) As String
    Dim names(5) As String
    Dim x As Integer

    names(0) = "Cherries"
    names(1) = "Oranges"
    names(2) = "Plums"
    names(3) = "Bells"
    names(4) = "Melons"
    names(5) = "Bar"

    For x = 0 To 2
        results(x) = names(CInt(Int((5 * Rnd()) + 1)))
    Next x

    Return results
End Function



Function DisplayResults(ByRef UserTokens, ByVal UserAnswer, ByRef number1, ByRef number2, ByRef number3)
    Console.WriteLine("*** " & number1 & " ** " & number2 & " ** " & number3 & " ***")
    If UserTokens > 0 Then
        Console.WriteLine("You have " & UserTokens & " tokens left, do you want to play again? (yes or no)")
        UserAnswer = (Console.ReadLine())
    Else
        Console.WriteLine("Sorry, you lost all of your tokens!")
    End If

    Return UserAnswer
End Function


End Module
Having trouble with function pointers and array

Having trouble with function pointers and array


By : user4369812
Date : March 29 2020, 07:55 AM
may help you . I am currently using Visual Studios for class and needless to say I am having difficulty understanding pointers. I have created a program but does not seem to work right for me. I keep getting an assertion error after I click the function type that I want. I get a Debug assertion failed pop up error. Expression: result_pointer != nullptr. It says line 1558 , This is why your code fails:
code :
scanf_s("%d", choice);
scanf_s("%d", &choice);
JavaScript: Creating a function to reject array elements found to be truthy, trouble with values in objects in the array

JavaScript: Creating a function to reject array elements found to be truthy, trouble with values in objects in the array


By : Zelka
Date : March 29 2020, 07:55 AM
around this issue Here's the solution I ended up with. To clarify, the tests were passing in arrays and objects, so that's why I first had trouble (with the objects) and then there was some confusion in in the answers. I wrote:
code :
function reject(collection, callback) {
    if(Array.isArray(collection)){
        for (var i = 0; i < collection.length; i++) {
            if(callback(collection[i]) === true){
                collection.splice(i, 1);
            }
        }
    } else {
        for (number in collection){
            if(callback(collection[number]) === true){
                delete collection[number];
            }
        }
    };

    return collection;
}
I'm having trouble in C++ geting input from a user in a function, adding to an array, and printing that array

I'm having trouble in C++ geting input from a user in a function, adding to an array, and printing that array


By : S.C.J
Date : March 29 2020, 07:55 AM
hope this fix your issue Your fundamental problem is that int getFourNums() can only return a single integer, not an array of them. The next problem is that functions cannot return raw arrays for historical reasons. Your choices are to return a std::array, a struct containing the array, pass the array by reference into the function, or return a std::vector. My preference for this application is a std::vector - it is flexible, and although not quite as efficient as std::array, you should probably default to std::vector unless you have a good reason otherwise. Your getNums code would then look like:
code :
std::vector<int> getFourNums() {
    std::vector<int> result;
    cout << "Enter 4 nums: ";
    for(int i = 0; i < 4; i++){
        int v;
        cin >> v;
        result.push_back(v);
    }
    return result;
}
Related Posts Related Posts :
  • N Queens Puzzle - Where is the Backtracking in this solution?
  • how to build a simple lock (mutex) on nios II cpu
  • C macro expansion of a function pointer based on for loop incrementor
  • Epoll with edge triggered and oneshot only reports once
  • how to implement (PHP Function)array_map funciton in c?
  • Find a sum of two distinct numbers from the set, closest to the query number
  • C, Piping messages to child processes not working. Why?
  • CreateThread() passing struct arguments weirdly
  • How to change a char to ASCII form?
  • Pipes, dup2 and exec()
  • I can't get this mean calculated, it just displays 0 all the time
  • Expected ' ' before '=' token in struct definition
  • How to implement a MATLAB lowpass filter in C
  • Convert hexidecimal char array to u8 array in C
  • Delete function in Binary Tree in C
  • Why (int)((unsigned int)((int)v)?
  • Creating a matrix in a structure
  • C Recursion runtime error
  • C: pointer to 2d array
  • How to free a pointer in a structure array?
  • gcc -O optimization: Help me understand the effect
  • Errors on code (state machine)
  • Does node equals node on a struct initializes the rest of the struct attributes?
  • C program to copy one string into other string without using library functions
  • Macro representing a pin on a microchip
  • C code workks in Mac (Darwin 13.4), but not in Linux (2.6.32)
  • How do I read this complex C declaration?
  • Reading a file with scanf and a do while loop
  • why is the recursion used in finding the factorial of a number?
  • Inverting array elements (bitwise) doesn't work
  • git blame-like annotation of source files with gcov
  • Error using pthread on Windows with Mingw
  • How to create a static library with a Makefile from C source code
  • Why are there two different ways of initializing a pointer in C
  • Program runs too slowly with large input - C
  • Dangling pointer example confusion
  • Why is this program running when input is 1?
  • Malloc() doesn't work + char array clear
  • Reading debug registers on linux
  • Socket Programming - Server content written to Client (write())
  • Incrementing a string in C
  • what does a[0] = addr & 0xff?
  • unexpected EOF while looking for matching `'' while using execve()
  • What's the purpose of stack pointer alignment in the prologue of main()
  • Time from startup in linux kernel
  • UNIX socket connection refused
  • How to programmatically set IP address on Windows 7 using C
  • programming a gpu without using any library like cuda or opencl?
  • C "printf" Different output in Linux and Mac
  • Bit rearrangement/manipulation in C
  • How to find occurences of a digit with 4 in it within 50?
  • Read string separated by comma
  • how to optimize the C code, when m is too big, it can't operation
  • GCC Error Infinite While Loop
  • Row-major vs Column-major confusion
  • Parsing memory mapped file C
  • going out of bounds in array of structs in C
  • fprintf in do while loop wrote only one line in file C
  • Reading and Writing to Files in C
  • Changable amount of scanned numbers per line (scanf)
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co